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);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况
  • ¥15 画两个图 python或R
  • ¥15 在线请求openmv与pixhawk 实现实时目标跟踪的具体通讯方法
  • ¥15 八路抢答器设计出现故障
  • ¥15 opencv 无法读取视频
  • ¥15 按键修改电子时钟,C51单片机
  • ¥60 Java中实现如何实现张量类,并用于图像处理(不运用其他科学计算库和图像处理库))
  • ¥20 5037端口被adb自己占了
  • ¥15 python:excel数据写入多个对应word文档