Hey! Persistently, but unsuccessfully trying to reach the server using curl . I want to get an answer but in the end I get a message about the api key error.

In short, I try to connect using the scheme as it is if I specifically try to translate this request into php. I make a request from the server without an ssl certificate.

 GET https://mercury.postlight.com/parser?url=https://trackchanges.postlight.com/building-awesome-cms-f034344d8ed Content-Type: application/json x-api-key: SWN6wongtJ6krFOa4MHwzRsAzHcaruPqGkrDqH8A 

I do it this way:

 <?php $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, 'https://mercury.postlight.com/parser'); curl_setopt($curl, CURLOPT_RETURNTRANSFER,true); curl_setopt($ch, CURLOPT_HTTPHEADER, array( 'Content-Type: application/json', 'x-api-key: ********************' )); curl_setopt($curl, CURLOPT_POSTFIELDS, 'url=http://www.velosite.ru/kak-vybrat-velosiped-dlja-vzroslogo/'); $out = curl_exec($curl); echo $out; curl_close($curl); 

To which I get in response:

 {"message":"Missing Authentication Token"} 

What needs to be changed? I don't understand something at all :(

1 answer 1

You initialize cURL into the $curl variable:

 $curl = curl_init(); 

But you pass the settings to the $ch variable:

 curl_setopt($ch, CURLOPT_HTTPHEADER, array( 'Content-Type: application/json', 'x-api-key: ********************' )); 

This is, in fact, a mistake.


I also advise you to remove the Content-Type: application/json header is trivial because you do not need it, and also pass the CURLOPT_POSTFIELDS to the CURLOPT_POSTFIELDS parameter not the query string, but the array:

 curl_setopt($curl, CURLOPT_POSTFIELDS, array('url' => 'http://www.velosite.ru/kak-vybrat-velosiped-dlja-vzroslogo/'); 

If you are already sending a POST request, do not forget to set the CURLOPT_POST parameter to true .