I am trying to convert this CURL command:
curl -i https://myurl >toto.txt
into a PHP script, but it doesn’t work.
$ch_rech = curl_init();
curl_setopt($ch_rech, CURLOPT_URL, "https://myurl");
curl_setopt($ch_rech, CURLOPT_HEADER, 0);
ob_start();
curl_exec($ch_rech);
curl_close($ch_rech);
$Results = ob_get_contents();
ob_end_clean();
How can I do it?
3
Answers
It's good, thank you for all. The problem was that my web server is blacklisted by the destionation server ;o)
Try so:
$resp contains the output (or false if there is an error)
$httpStatus contains (obviously 🙂 ) the HTTP status code returned
The question suggests that you wish to write the response of the curl request to a text file yet the comment suggests totally the opposite. The URL you are using is
https
so one has to usually supply a few more parameters to the curl request, such as the public SSL certificate info for which you can download a copy of cacert.pemTo write the output to a file:
To display the response directly:
CURLOPT_FOLLOWLOCATION
– useful if the server sendslocation
headers ( if the original url has been changed for example )CURLOPT_RETURNTRANSFER
– when you want to capture the response from the request into a variable.In the latter example above by setting
CURLOPT_RETURNTRANSFER
and capturing the http response code by using$status=curl_getinfo( $curl, CURLINFO_RESPONSE_CODE );
we can fork any logic needed to display/parse/scrape content