访问CURL请求+PHP的结果



我正在使用php:执行一个curl请求

  //Initiate cURL.
  $ch = curl_init($url);
  //Encode the array into JSON.
  $jsonDataEncoded = json_encode($data);
  //Tell cURL that we want to send a POST request.
  curl_setopt($ch, CURLOPT_POST, 1);
  //Attach our encoded JSON string to the POST fields.
  curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonDataEncoded);
  //Set the content type to application/json
  curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
  //Execute the request
  $result = curl_exec($ch);

浏览器返回:

{"success":"true","lead_id":"141872","code":201}
OR
{"error":true,"duplicate":true,"message":"Duplicate: This has already been submitted to the API.","code":400}

我如何访问$result中的数据它当前仅返回1

将return transfer设置为true以在$result 中获得页面响应

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$options = array (
    CURLOPT_CONNECTTIMEOUT => 1, // timeout on connect
    CURLOPT_TIMEOUT => 1, // timeout on response
    CURLOPT_MAXREDIRS => 1 ,
    CURLOPT_HTTPHEADER => array("REMOTE_ADDR: $proxy", "HTTP_X_FORWARDED_FOR: $proxy"),
    CURLOPT_URL => $url 
    );
$ch = curl_init();
curl_setopt_array ( $ch, $options );
$result = curl_exec($ch);
print_r($result);
curl_close($ch);

您的解决方案:

最新更新