3

I have some PHP code that calls into the cURL library. I'm using it to get JSON data.

I have set cURL opt 'CURLOPT_RETURNTRANSFER' to 1, but still get status code..

Code follows:

<?php
function fetch_page($url)
{

    $ch = curl_init();

    $array = array(
        'include'=>'ayam'
    );

    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $array);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

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

    return $result;
} 

$return  = fetch_page(MY_LINK_URL);

echo json_decode($return);
?>

6 Answers 6

4

The code looks totally correct. Try var_dump($result) before returning it to see what it is exactly.

Also, set CURLOPT_HEADER to 1 and check the view source of the output in your browser; both of these can help debug the issue. Edit the question and post the results if so we can help more effectively.

Update: Since you are using HTTPS, also add

curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
Sign up to request clarification or add additional context in comments.

1 Comment

@pensilhijau: Have a look at the update, maybe that will help?
4

That looks correct. I actually have the same problem, but when I added

curl_setopt($ch,  CURLOPT_RETURNTRANSFER, 1);

it returns json code correctly instead of returning 1(True).

Comments

3

According to the PHP docs,

Returns TRUE on success or FALSE on failure. However, if the CURLOPT_RETURNTRANSFER option is set, it will return the result on success, FALSE on failure.

So that means you should get success: the result failure: FALSE (which is echoed as 0)

Also, if you are fetching JSON, and need to access it, use json_decode() not json_encode().

Comments

0

Well, you should tell us wich url you're pointing (and wich version of php you are running). I've tried with php 5.3 on "www.google.com" and it worked as expected ($result contains the whole webpage)

2 Comments

the url that i pointing is use https, is problem with that? and im use php 5.1
No, it should be no problem...are you completly sure the page you are pointing is OK?
0

Might of had a similar problem: - cURL on local dev network problem with virtual host naming

Before you close your curl handle output this:

$result = curl_exec ($ch);
print_r(curl_getinfo($ch));
curl_close ($ch);

Here was my solution for getting around the virtual host

// This is your Virtual Hosts name
$request_host   = 'dev.project'; 

// This is the IP
$request_url    = '192.168.0.1';

$headers = array("Host: ".$request_host);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);     
curl_setopt($ch, CURLOPT_URL, $request_url.'?'.$request_args);
curl_setopt($ch, CURLOPT_HEADER, FALSE);

Comments

0

There seems to be a number of reasons why CURLOPT_RETURNTRANSFER can be ignored, and SSL certificate checking is only one of them:

In my case the culprit was CURLOPT_POST which I had set to true. I was expecting to get back a string consisting of the HTTP response header plus the response itself. Instead I was getting status code 1. Go figure. Thankfully I did not need the HTTP header so the solution for me was:

curl_setopt($ch, CURLOPT_HEADER, false);

If I did need the header info, I don't know what I would do. I've wasted an insane amount of time tracking down the problem.

Damn you PHP curl! (waves fist in anger)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.