PHP Curl Print Problem - php

I'm trying to print an external site's HTML using curl, but when I go to print the html I just get a bunch of bad characters:
D\ÓLþ¢GΖ´ï!ñ{HÑ,Jþ»H¹§L+÷53j?‰²î¡<‘*tÜe÷uÖbìê~Æô¬²c˜‹§ ~áäÆL#f?ⶊªþU™á˜ÉÉOæ{^¤ëaÀ Tê"1Û¨Dtî’œxˆk‘:#ŽD5î:'¶e\*³q‘׸`…±¾ôäó÷ð1j7þä‘åQ6®9bcxã„A2ã—-ÇøüåÉò÷2{ÂÐe桢ǙŒÄg©Az!Ø¡>±zךÂ+;f RZÛÝ€ížáÒžHa¬¢Æ'ë•ñ þ=Ð=ºtyšÖâå'ÇpžÄ¦ÆN½€5½roåðFe¹)ˆš`ØnhŠy(GÆÔ} Bu7H¥JzÐ iVê÷áÆ”øG>6HÿUµÞhµj YH-ÌaaEÚx±‰…Êâ£-ûeÎqCÆLÌå㘎‰†LÐÆM I]€a)Ï.$—ÅH••£ ŒŒ’ªÉ;cŽc(i´¥!I]‹€„³tFc^ë'€e±øÉ_øß
Here is my code:
$curl = curl_init();
curl_setopt ($curl, CURLOPT_URL, "http://www.thesite.com/");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec ($curl);
curl_close ($curl);
print $result;
Any help would be great,
Thanks!

If the response is GZIP'd, you may use this:
curl_setopt($curl, CURLOPT_ENCODING, 'gzip');
See: http://www.php.net/manual/en/function.curl-setopt.php
To see if it's gzip'd, check the headers (see CURLOPT_HEADER).

Related

cURL via PHP works on certain sites and on some others don't work?

<?php
$url = 'https://adidas.com/us/';
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HEADER, false);
$data = curl_exec($curl);
curl_close($curl);
echo $data;
?>
I'm just trying to display the contents via cURL, and when I try https://www.google.com/ or another website, it works just fine, however on the adidas site, it timesout?
Thanks!
Add a curl_getinfo call and you'll see more useful information
<?php
$url = 'https://adidas.com/us/';
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HEADER, false);
$data = curl_exec($curl);
print_r(curl_getinfo($curl));
curl_close($curl);
echo $data;
?>
In this case, after a minute it times out and curl_getinfo returns http_code of 0. This means that a connection can not be established.
adidas.com is apparently inaccessible through HTTPS. It works through HTTP and redirects to http://www.adidas.com/us/. This address works through HTTPS too.
Use https://www.adidas.com/us/ instead.
You get an empty string for curl_exec (for http link) because response code is 301 Moved Permanently. Body of the HTTP response is empty.

php curl function not giving the required result

I need help with API Integration. When I echo the variable $url, I get the result, but I do not know why cURL is not working for me:
<?php
$token="43e6c623dda8f35df4bXXXfa5f0ec57d58e91154a ";
$format="json";
$waybill="974510010010";
$ref_nos="";//either this or waybill
$verbose="0";// meta info need to append in url
$url="https://test.delhivery.com/api/packages/json/?token=".$token."&format=".$format."&waybill=".$waybill."&ref_nos=".$ref_nos."&verbose=".$verbose;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPGET, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
//curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
echo curl_error($ch);
$return = curl_exec ($ch);
curl_close ($ch);
echo $return;
?>
Do you have access to the service you are connecting to?
It could be that the service requires the data as POST variables as opposed to the GET parameters you are sending.
Or perhaps some error handling on server that does not validate the waybill value you are sending. The ref_nos variable is also empty, which some applications could interpret as invalid. A tips would be to omit the ref_nos variable from the request string if its value is empty.

How to get value of variable from curl in PHP?

I am working on two system.In which asterisk runs on one system-1.I want to run command in asterisk and get result back in system-2.I make curl request like below.How to get value back on system2?enter code here
exec('asterisk -rx "sip show peers"',$sip);
$POST_DATA = array(
'filename'=>$sip,
);
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL,'http://192.168.50.138/test.php');
curl_setopt($curl, CURLOPT_TIMEOUT, 30);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $POST_DATA);
$response = curl_exec($curl);
curl_close ($curl);
?>
Since you already have
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
in your code. curl_exec should already returns the content of the page instead of a BOOL.
This is a snippet of a library I use. As pointed out this might not be needed but it helped me out once...
//The content - if true, will not download the contents
curl_setopt($ch, CURLOPT_NOBODY, false);
Also it seems to have some bugs related to CURLOPT_NOBODY (which might explain why you have this issue):
http://osdir.com/ml/web.curl.general/2005-07/msg00073.html
http://curl.haxx.se/mail/curlphp-2008-03/0072.html

how to parse a list returned by a link using php

I do have this link : 'http://www.domainname.com/employee=name'
when I put it on the browser it return this output:
["steve", "charles", "Micheal"]
How do I curl this link using php
Thanks,
$curl = curl_init();
curl_setopt ($curl, CURLOPT_URL, "$URL");
$ANSWER=curl_exec ($curl);
curl_close ($curl);
Try this:
$output=json_decode(file_get_contents("http://www.domainname.com/employee=name"),true);

Display Number up to two decimal point error

I want to dislay the number up to 2 decimal point the output is coming from the xml file through curl function. I tried the number_format, printf('%.2f',$number)and sprintf() functon.these are displaying the result 0.00.The code is following.
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, `http://orbisadvisors.redinews.com/tools/XM01? queryid=QJ33020&fields=Last&fields=Change&fields=Chperc&symbol=BSZ`);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$sp= curl_exec ($ch);
$sp1=explode(" ",$sp);
echo "????".$lsp1=$sp1[4];
printf("%.2f",$lsp1);
print_r($sp1);
curl_close ($ch);
In case I use the static number in place of the variable .It is giving the correct result.
Thanks
You should be using a library like SimpleXML to parse the XML data that you're receiving, like the following:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://orbisadvisors.redinews.com/tools/XM01?queryid=QJ33020&fields=Last&fields=Change&fields=Chperc&symbol=BSZ');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$sp= curl_exec ($ch);
curl_close ($ch);
$xml = simplexml_load_string($sp);
$last = sprintf("+%.2f", $xml->Stock->Last); // 1280.70

Categories