質問

I looked through all of the other terminal vs. PHP questions and couldn't find a solve.

I'm working with data API and this works to retrieve a list of files:

What am I missing in the translation?

PHP CODE:
$url = 'https://api-url';
$data = array(
    'header' => array(
        'id' => '1',
    ),
    'others' => array(
        "details" => yes,
    ),    
);

$data_string = json_encode($data);                                                                                 


$ch = curl_init($url);                                                                      
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);                                                                  
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);                                                                      
curl_setopt($ch, CURLOPT_HTTPHEADER, array(                                                                          
    'Content-Type: application/json',                                                                                
    'Content-Length: ' . strlen($data_string))                                                                       
);                                                                                                                   

$result = curl_exec($ch);
var_dump($result);

Terminal code:(Working fine)

curl -X POST -d '{"header": {"id": 1}, "others": {"details": yes}' --user "u-p" 'https://api-url'
役に立ちましたか?

解決 2

try to add for ssl

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);

Also you need to check your API host and sending data it's giving error like:-

"{"result":"failed","errorMessage":"expected HTTP authorization, \"client_id\" property, or \"integration\" property","errorCode":"system.error"}" 

他のヒント

1) You forgot to translate --user option. Its counterpart in PHP libcurl is CURLOPT_USERPWD option.

2) As has already written in the comments you have to disable SSL peer verification by setting CURLOPT_SSL_VERIFYPEER option to false.

3) You shouldn't set CURLOPT_CUSTOMREQUEST and CURLOPT_POST options together, only one of them (CURLOPT_POST in your case).

So the code should look like this:

...
$ch = curl_init($url);
curl_setopt_array($ch, array(
    CURLOPT_RETURNTRANSFER => 1,
    CURLOPT_SSL_VERIFYPEER => false,
    CURLOPT_USERPWD => 'username/password',
    CURLOPT_POST => 1,
    CURLOPT_POSTFIELDS => $data_string,
    CURLOPT_HTTPHEADER => array(
        'Content-Type:application/json',
        'Content-Length: ' . strlen($data_string)
    ),
));
$result = curl_exec($ch);
var_dump($result);
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top