Reputation: 81
There must be an error in my PHP script but it says 'OK' in the text that us returned. Why does it come out in the error channel instead of getting put into the variable where I could use it? This is my code:
<?php
$url = 'https://rest.tsheets.com/api/v1/jobcodes';
$api_token ="********";
$headers = array();
$headers[] = "Authorization: Bearer {$api_token}";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, true);
$o = curl_exec($ch);
echo "Error:" . curl_error($ch);
echo "This is output: " . var_dump($o);
?>
The output looks like this:
Error:string(23256) "HTTP/1.1 200 OK
Date: Fri, 22 Jul 2016 15:18:14 GMT
Server: Apache
Cache-Control: no-cache, must-revalidate
Expires: 0
Transfer-Encoding: chunked
Content-Type: application/json
{
"results": {
"jobcodes": {
"493387": {
"id": 493387,
"parent_id": 0,
"assigned_to_all": false,
"billable": false,
"acti
.
.
.
"more": true
}"
This is output:
So nothing is put into the variable being returned, even though returntransfer is set. I cannot work out whether there really is an error or whether it is a PHP.ini setting that is causing it. The fact that it is returning OK suggests the script is fine. The script works when it is hosted on a web server but this is now running from my PC in Windows and cUrl is not returning the data. Can anyone advise what I need to fix?
Upvotes: 3
Views: 116
Reputation: 81
Thanks Guys, You were right. I was obscuring the error message by printing out the error first. As soon as I used cambierr's method I saw the error which related to certificates. I found my php was only specifying the directory where the cacert file was and not the whole path including the filename. It's all working now. Thanks for your help, Neil
Upvotes: 0
Reputation: 421
Wrong: the var_dump() method does not returns a String but print it.
So what you're doing here is printing "Error:".
Then printing $o's content.
Then "This is output: ".
To test for an error:
if($o){
echo "This is output: \n";
var_dump($o);
} else{
echo "Error:" . curl_error($ch);
}
Upvotes: 1