访问CURL请求+ PHP的结果

时间:2016-07-21 10:45:47

标签: php curl

我正在使用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

2 个答案:

答案 0 :(得分:2)

将return transfer设置为true以获取$ result

中的页面响应
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

答案 1 :(得分:1)

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

你的解决方案: