如何将curl转换为php cURL

时间:2017-06-03 19:34:36

标签: php curl

我已经引用并尝试过以前的stackoverflow问题的建议,尝试将此类型的CURL请求转换为php但失败:返回错误请求。我也读了它并尝试了以下以及其他尝试: 这种类型的请求有效:

$ curl -X POST \
  -u f98b80bd3aa9dd8:748b2a74-cf59-4afb-facf-b79f3ba51e46 \
  -H 'Content-Type:application/json' \
  -d '{"hash": "fa814e37ad092518b0b22ed1f21c8bac4daed663433abba01e369399522279e6"}'
  https://api-prod.stamp.com/stamps

我最清楚的尝试:

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://api-prod.stamp.com/stamps');
curl_setopt($ch, CURLOPT_HTTPHEADERS, array('Content-Type:application/json'));
curl_setopt($ch, CURLOPT_USERPWD, 'f98b80bd3aa9dd8:748b2a74-cf59-4afb-facf-b79f3ba51e46');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, '{"hash":"fa814e37ad092518b0b22ed1f21c8bac4daed663433abba01e369399522279e6"}');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$result = curl_exec($ch);

curl_close($ch);
print_r($result);

也尝试过:

$url = 'https://api-prod.stamp.com/stamps';
//open connection
$ch = curl_init();

//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('content-type: application/json'));
$json = json_encode('{f98b80bd3aa9dd8:748b2a74-cf59-4afb-facf-b79f3ba51e46"}');
curl_setopt($ch,CURLOPT_POSTFIELDS, $json);

//execute post
$result = curl_exec($ch);

//close connection
curl_close($ch);

更新了cBroe的建议,改进但仍然不对:更新

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://api-prod.stamp.com/stamps');
curl_setopt($ch, CURLOPT_HTTPHEADERS, array('Content-Type:application/json'));
curl_setopt($ch,CURLOPT_USERNAME, 'f98b80bd3aa9dd8');
curl_setopt($ch, CURLOPT_USERPWD, '748b2a74-cf59-4afb-facf-b79f3ba51e46');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, '{"hash":"fa814e37ad092518b0b22ed1f21c8bac4daed663433abba01e369399522279e6"}');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$result = curl_exec($ch);

curl_close($ch);
print_r($result);

0 个答案:

没有答案