Curl Not Returning Anything

cURL not returning anything?

It does, if you set a complete URL with a '/' at the end (fix two other typos):

error_reporting(-1);
$config = array
(
"siteURL" => "http://www.apple.com/",
"loginCheck" => "checkuser.php",
"userAgent" => "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/534.16 (KHTML, like Gecko) Chrome/10.0.648.204 Safari/534.16"
);

$postFields = "username=user&password=pass&submit= Login ";

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $config['siteURL'] . $config['loginCheck']);
curl_setopt($ch, CURLOPT_COOKIEJAR, "cookie.txt");
curl_setopt($ch, CURLOPT_COOKIEFILE, "cookie.txt");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_USERAGENT, $config['userAgent']);
curl_setopt($ch, CURLOPT_REFERER, $config['siteURL']);

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

echo $content;

Simple cURL not returning anything

Try like this,



$curl = curl_init();

curl_setopt_array($curl, array(
CURLOPT_URL => "http://webgis.dor.wa.gov/webapi/addressrates.aspx?output=xml&addr=416%20Sid%20Snyder%20Ave%20SW&city=Olympia&zip=98504",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_HTTPHEADER => array(
"cache-control: no-cache",
),
));

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

if ($err) {

echo "cURL Error #:" . $err;

} else {

echo $response;

// convert it to array if you want
$xml = simplexml_load_string($response);
$json = json_encode($xml);
$arr = json_decode($json,true);
print '<pre>';
print_r($arr);
print '</pre>';

}

PHP CURL curl_exec not returning anything

Based on the headers returned by your curl_getinfo() call, specifically [http_code] => 301, the request is being redirected but you haven't told curl it's allowed to follow it.

To do this, you'll have to set the CURLOPT_FOLLOWLOCATION to true:

curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);

Also, as it's relevant, the URL it's redirecting to is http://www.hazelandruby.com/blog/feed/, your original URL with an appended /. You should be able to "fix" the issue by adding the ending / to the original URL (hopefully =P).

PHP cURL GET Request to REST API not returning anything

The API expects a GET request, but using curl_setopt($cURLConnection, CURLOPT_POSTFIELDS, $params); is changing the request type to POST. removing this command and adding the parameters directly to the URL fixes the problem.

cURL GET Request Returns No Output

Check the headers: It's only accessible by https:

$ curl --dump-header - http://pinterest.com/
HTTP/1.1 302 FOUND
Accept-Ranges: bytes
Age: 0
Content-Type: text/html; charset=utf-8
Date: Thu, 18 Jul 2013 19:25:49 GMT
Etag: "d41d8cd98f00b204e9800998ecf8427e"
Location: https://pinterest.com/
Pinterest-Breed: CORGI
Pinterest-Generated-By: ngapp-b7f64694
Pinterest-Version: a8eef3c
Server: nginx/0.8.54
Set-Cookie: csrftoken=A2VQZGarr509JKxrJxiuW2MbrXNdHlUH; Domain=.pinterest.com; expires=Thu, 17-Jul-2014 19:25:49 GMT; Max-Age=31449600; Path=/
Set-Cookie: _pinterest_sess="eJwz84isyvfJcilP1S4szHY20A6MKitJKwwPdi+2tY8vycxNtfUN8TX2c3E19gsJNfAPtLVVK04tLs5MsfXMcjTxq/KsAGJj3/CgHL+QoGzfrLCMSKNAIz93X+PIrHQTIF0eFe6X4ZluawsAh3UjNA=="; Domain=.pinterest.com; expires=Sun, 13-Jul-2014 19:25:49 GMT; Max-Age=31103999; Path=/
Vary: Cookie
Via: 1.1 varnish
X-Varnish: 1991078486
Content-Length: 0
Connection: keep-alive

If you use the -L option you'll get the page:

$ curl -L http://pinterest.com/
<!DOCTYPE html>
<!--[if IE 7 ]><html lang="en" class="ie7 ielt9 ielt10 en"><![endif]-->
<!--[if IE 8 ]><html lang="en" class="ie8 ielt9 ielt10 en"><![endif]-->
<!--[if IE 9 ]><html lang="en" class="ie9 ielt10 en"><![endif]-->
<!--[if (gt IE 9)|!(IE)]><!--><html lang="en" class=" en"><!--<![endif]-->

<head>
<script>
[snip]

Here in this link is how it's done with PHP:

[Years later...] also, -V is --version, not --verbose, which is lowercase -v. invoking curl -V causes it to display the version and ignore any args, so you would never get the page that way anyway.

Curl doesn't return anything

Curl is telling you you are being redirected to /spaces/enter.

You can tell Curl to automatically follow redirects:

curl -vL [url]

Curl showing but not returning data

The documentation of curl_exec() says:

Return Values

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.

What it doesn't say is explained in the documentation page of curl_setopt(), on the CURLOPT_RETURNTRANSFER option:

Option: CURLOPT_RETURNTRANSFER

Set value to: TRUE to return the transfer as a string of the return value of curl_exec() instead of outputting it out directly.

That is, by default, curl_exec() outputs the body of the response it gets. In order to make it return the value and not output it, you have to use curl_setopt():

$ch = curl_init($url);
curl_exec($ch, CURLOPT_RETURNTRANSFER, TRUE);
$result = curl_exec($ch);
curl_close($ch);

PHP cURL (Generated on Postman) not returning anything

It was a stupid error, and it had nothing to do with the code.

One of the first things I checked was that PHP had cURL module installed, but I didn't notice that it was disabled on php.ini, so the error showed in the only log I didn't checked, apache error.log.

After the module was enabled it worked like a spell.

Thank you all that tried to help me in this.



Related Topics



Leave a reply



Submit