dongshuql24533 2017-12-06 05:22
浏览 28
已采纳

在php中运行curl

I want to execute Curl code inside PHP.

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'   

Above code has four parameters to pass to the api. Below PHP code I tried to execute:

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);
}   

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] => No media specified ) )

Now the problem is fro the media and models parameters.

  1. I'm not sure the file path to media parameter has some problem. And do I want to and an additional @ in-front of the path.

  2. I'm I defined all parameters in $body_data array and passing them to the CURLOPT_URL properly.

Please help me to solve this issue. When I try this in POSTMAN it works fine. Thanks in advance.

  • 写回答

1条回答 默认 最新

  • dqpciw9742 2017-12-06 06:05
    关注

    You don't need to use http_build_query.

    PHP 5.5, 5.6 etc supported to pass @ sign, but deprecated in PHP 7. Now we can use 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);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么