cURL to PHP cURL


cURL to PHP cURL

我在将cURL转换为PHP cURL时遇到了一个小问题。

cURL是:

curl -X POST https://api.curated.co/PUBLICATION_KEY/api/v1/email_subscribers -H 'Accept: application/json' -H 'Content-type: application/json' -H 'Authorization: Token token="API_KEY"' -d '{ "email" : "new_subscriber@example.com" }'

我正在将其转换为:

<?php 
$headers = array(
    'Accept: application/json',
    'Content-type: application/json',
    'Authorization: Token token="API_KEY"'
);
$data = array("email" => "new_subscriber@example.com");                                                                    
$data_string = json_encode($data);
$ch = curl_init('https://api.curated.co/PUBLICATION_KEY/api/v1/email_subscribers');                                                                      
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");                                                                     
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);                                                                  
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);                                                                      
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);       
$result = curl_exec($ch);
?>

但我没有得到任何回应。注意:我正在用正确的API_KEYPUBLICATION_KEY进行更改。

我遵循本指南:http://support.curated.co/hc/en-us/articles/201753981-Adding-Subscribers-with-the-API

如果我遗漏了什么,你能告诉我吗?

我肯定会用curl_setopt($ch, CURLOPT_POST, true);替换curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");如果这不会改变任何事情,你可能还想添加curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);,以防Curated涉及重定向(也许他们有一个单独的身份验证层,然后将你转发到所需的端点)

试试这个(:

将Curl命令转换为PHPhttps://incarnate.github.io/curl-to-php/

curl_setopt($ch,CURLOPT_URL,"https://api.curated.co/PUBLICATION_KEY/api/v1/email_subscribers");curl_setopt($ch,CURLOPT_RETURNTTRANSFER,1);curl_setopt($ch,CURLOPT_POSTFIELDS,"{''"电子邮件''":''"new_subscriber@example.com''"}");curl_setopt($ch,CURLOPT_POST,1);$headers=数组();$headers[]="Accept:application/json";$headers[]="内容类型:application/x-www-form-urlencoded";$headers[]="授权:令牌令牌=''"API_KEY''";curl_setopt($ch,CURLOPT_HTTPHEADER,$headers);$result=curl_exec($ch);if(curl_errno($ch)){echo"错误:"。curl_error($ch);}curl_close($ch);