【问题标题】:Run curl inside php在 php 中运行 curl
【发布时间】:2017-12-06 05:22:28
【问题描述】:

我想在PHP 中执行Curl 代码。

curl -X POST 'https://api.sightengine.com/1.0/check.json' \
     -F 'api_user=1********5' \
     -F 'api_secret=q**************Q' \
     -F 'media=@/full/path/to/image.jpg' \
     -F 'models=nudity'   

上面的代码有四个参数要传递给api。下面是我尝试执行的 PHP 代码:

function image()
{
    $body_data = http_build_query(array('api_user' => 1********5,
                       'api_secret' => 'q**************Q',
                       'media' => $_FILES['image']['name'],
                       'models' => 'nudity'));

    // Configure cURL
    $image_curl = curl_init();
    curl_setopt($image_curl, CURLOPT_URL, "https://api.sightengine.com/1.0/check.json");
    curl_setopt($image_curl, CURLOPT_POST, true); // Use POST
    curl_setopt($image_curl, CURLOPT_POSTFIELDS, $body_data); // Setup post body
    curl_setopt($image_curl, CURLOPT_RETURNTRANSFER, true); // Receive server response

    // Execute request and read responce
    $session_response = curl_exec($image_curl);
    $response = json_decode($session_response);

    print_r($response);
}   

回应:

stdClass Object ( [status] => failure [request] => stdClass Object ( [id] => req_2365jHPuLcC6Bydh7WNd7 [timestamp] => 1512542730.57 [operations] => 0 ) [error] => stdClass Object ( [type] => argument_error [code] => 4 [message] => 没有指定媒体 ) )

现在问题出在mediamodels 参数上。

  1. 我不确定media 参数的文件路径有问题。我是否想在路径前面加上一个额外的@

  2. 我在$body_data 数组中定义了所有参数,并将它们正确传递给CURLOPT_URL

请帮我解决这个问题。当我在POSTMAN 中尝试此操作时,它工作正常。提前致谢。

【问题讨论】:

  • 你见过这个吗? github.com/Sightengine/client-php - 没有 Guzzle 79 行代码..
  • 我可以用它来上传图片吗?表示在它上传到服务器之前。
  • 我是否只需要在我的工作项目中安装这个client-php。我实际上没有得到它。请帮我这样做。我在我的项目中使用 codeigniter 框架。

标签: php api curl post


【解决方案1】:

你不需要使用http_build_query

PHP 5.5、5.6 等支持传递@ 符号,但在 PHP 7 中已弃用。现在我们可以使用 https://secure.php.net/manual/en/class.curlfile.php

$body_data = array(
    'api_user' => '3454',
    'api_secret' => 'q**************Q',
    'models' => 'nudity'
);

$body_data['media'] = new CurlFile(realpath('file.jpg'));

// Configure cURL
$image_curl = curl_init();
curl_setopt($image_curl, CURLOPT_URL, "https://api.sightengine.com/1.0/check.json");
curl_setopt($image_curl, CURLOPT_POST, true); // Use POST
curl_setopt($image_curl, CURLOPT_POSTFIELDS, $body_data); // Setup post body
curl_setopt($image_curl, CURLOPT_RETURNTRANSFER, true); // Receive server response

// Execute request and read responce
$session_response = curl_exec($image_curl);
$response = json_decode($session_response);

【讨论】:

  • 我正在使用的image() 函数上面有什么问题吗?卷曲执行适用于其他一些 api 调用。但是当我尝试提供上传图片路径时失败。
  • 您使用的是文件名$_FILES['image']['name']。您应该使用文件存在的位置 $_FILES['image']['tmp_name'] 。您应该在路径前面附加@,因此它将是`'media' => @。 $_FILES['image']['tmp_name']` 。希望对您有所帮助。
  • 我试过这样'media=' => '@/'.$_FILES['image']['tmp_name'],但仍然是同样的错误。
  • 你能告诉我为什么你在@之后添加了/吗?请尝试使用真实文件而不是上传文件。也发布错误。
  • 我已更新代码以使用 CurlFile。所以它应该适用于所有 php 版本。作为记录,有一个名为CURLOPT_SAFE_UPLOAD 的标志,它使用@ 符号表示要上传的文件。 php.net/manual/en/function.curl-setopt.php 上的详细信息
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-03-18
相关资源
最近更新 更多