Retrieving url feed with curl return empty? - php

I need to retrieve the XML content from this URL :
http://www.ardmasr.com/MasrenaRss.aspx?Category=4
always return an empty string with out any errors, code:
$curl = curl_init();
curl_setopt ($curl, CURLOPT_CONNECTTIMEOUT,10);
curl_setopt ($curl, CURLOPT_USERAGENT,'smartic-rss-feeds');
curl_setopt ($curl, CURLOPT_URL, $url);
curl_setopt ($curl, CURLOPT_TIMEOUT,120);
curl_setopt ($curl, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt ($curl, CURLOPT_SSL_VERIFYPEER, 0);
if($curlFollowRedirects){
curl_setopt($curl, CURLOPT_HEADER, true);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, false);
}
$content = curl_exec($curl);
// Check if any error occurred
if($content === false)
{
echo 'Curl error: ' . curl_error($curl);
}
curl_close ($curl);
// if there is a redirect
if(preg_match('#Location: (.*)#',$content, $r)){
$l = trim($r[1]);
$content = $this->fetch_data($l);
}
return $content;

My problem in encoding the URL i used this function to fix my problem :
utf8_decode($url)

Related

cURL returning empty string

Hey am I missing a curl_opt or something? This code was working yesterday and now it doesnt work. It is suppose to return a json string but instead returns empty string.
<?php
session_start();
$UCID=$_POST['UCID'];
$Pass=$_POST['Pass'];
$credentials=array("Username"=>$UCID, "Password"=>$Pass);
$post_field_string = http_build_query($credentials, '', '&');
$url1="https://web.njit.edu/~vm276/connect.php";
//$url1="https://www.njit.edu/cp/login.php";
$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, $url1);
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt ($ch, CURLOPT_TIMEOUT, 60);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_PROTOCOLS, CURLPROTO_HTTPS);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt ($ch, CURLOPT_REFERER, $url1);
curl_setopt ($ch, CURLOPT_POSTFIELDS, $post_field_string);
curl_setopt ($ch, CURLOPT_POST, true);
$result = curl_exec($ch);
curl_close($ch);
$json=json_decode($result);
echo $json->result;
?>
You can get information about errors by adding to your code:
$result= curl_exec($ch);
$error = curl_error($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if(200 == $httpCode) {
//...
}
else {
echo "httpCode: ".$httpCode."\n";
var_dump($error);
exit();
}
http://php.net/manual/en/function.curl-error.php
http://php.net/manual/en/function.curl-getinfo.php

How to debug a get request in php using curl

I'm trying to make a get request in php using curl. This is what I'm doing:
$curl = curl_init();
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($curl, CURLOPT_USERPWD, "username:password");
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($curl);
curl_close($curl);
printf($result);
But $result doesn't print out anything, no success or failure message. I've successfully reached the endpoint via postman and in a web browser so I know it works. Printing out $curl prints: "Resource #1" which makes me think curl is properly installed on the server.
I'm not sure what steps to take next to make things work.
Add a few more option for troubleshooting purposes.
Check for an error response.
If no error, get the details:
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($ch, CURLOPT_TIMEOUT,10);
curl_setopt($ch, CURLOPT_FAILONERROR,true);
curl_setopt($ch, CURLOPT_ENCODING,"");
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
curl_setopt($ch, CURLOPT_HEADER, true);
$data = curl_exec($ch);
if (curl_errno($ch)){
$data .= 'Retreive Base Page Error: ' . curl_error($ch);
}
else {
$skip = intval(curl_getinfo($ch, CURLINFO_HEADER_SIZE));
$head = substr($data,0,$skip);
$data = substr($data,$skip);
$info = curl_getinfo($ch);
$info = var_export($info,true);
}
echo $head;
echo $info;
You can utilize curl's CURLOPT_VERBOSE and CURLOPT_STDERR like this:
$curl = curl_init();
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($curl, CURLOPT_USERPWD, "username:password");
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$verbose = fopen('php://temp', 'w+');
curl_setopt($curl, CURLOPT_VERBOSE, true);
curl_setopt($curl, CURLOPT_STDERR, $verbose);
$result = curl_exec($curl);
curl_close($curl);
rewind($verbose);
$verboseLog = stream_get_contents($verbose);
echo "Verbose information:\n<pre>", htmlspecialchars($verboseLog), "</pre>\n";
printf($result);

Curl Output not showing

Below is my code but it's not showing output. Now I temporarily shown yahoo.com as the curl output but my actual output url is different. Can anyone tell me what's the problem in the code?
<?php
$myurl = "www.yahoo.com";
curl_setopt ($ch, CURLOPT_URL, $myurl);
curl_setopt ($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result= curl_exec ($ch); //execute and get the results
echo $result; //display the reuslt
curl_close($ch);
?>
Try This:
//Add $ch = curl_init();
$ch = curl_init();
$myurl = "www.yahoo.com";
curl_setopt ($ch, CURLOPT_URL, $myurl);
curl_setopt ($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result= curl_exec ($ch); //execute and get the results
echo $result; //display the reuslt
curl_close($ch);
Your are not initializing the curl use this:
<?php
$myurl = "www.yahoo.com";
$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, $myurl);
curl_setopt ($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result= curl_exec ($ch); //execute and get the results
echo $result; //display the reuslt
curl_close($ch);
?>

How to get data from other website to my website using CURL

I want get data from this website: https://vrl.lta.gov.sg/lta/vrl/action/pubfunc?ID=EnquireTransferFee
This is my code using CURL:
function postPage($url, $pvars, $referer, $timeout){
if(!isset($timeout))
$timeout=30;
$curl = curl_init();
$post = http_build_query($pvars);
if(isset($referer)){
curl_setopt ($curl, CURLOPT_REFERER, $referer);
}
curl_setopt ($curl, CURLOPT_URL, $url);
curl_setopt ($curl, CURLOPT_TIMEOUT, $timeout);
curl_setopt ($curl, CURLOPT_USERAGENT, sprintf("Mozilla/%d.0",rand(4,5)));
curl_setopt ($curl, CURLOPT_HEADER, 0);
curl_setopt ($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($curl, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt ($curl, CURLOPT_POST, 1);
curl_setopt ($curl, CURLOPT_POSTFIELDS, $post);
curl_setopt ($curl, CURLOPT_HTTPHEADER,
array("Content-type: application/x-www-form-urlencoded"));
$html = curl_exec ($curl);
curl_close ($curl);
return $html;
}
$vars = array(
'vehicleNo' => 'SFT4228H',
'transferDate' => '10042014'
);
$result = postPage('test.php', $vars, 'https://vrl.lta.gov.sg/lta/vrl/action/pubfunc?ID=EnquireTransferFee', '30');
print "Result:".$result; //Show data in my website
=> How to get this data
As you have it here, it appears that you are using curl to request "test.php".
Try switching them around in your function call:
$result = postPage('https://vrl.lta.gov.sg/lta/vrl/action/pubfunc?ID=EnquireTransferFee', $vars, 'test.php', '30');

SAASU API not returnig response using rest api

I sending request to SAASU to create new contact using REST API but i am not getting result.
Please help me.
Method-1
$web_service = 'https://secure.saasu.com/webservices/rest/r1/Tasks?wsaccesskey=XXXXXXXXXXXXXXXXX&FileUid=XXX';
$xml = '<?xml version="1.0" encoding="utf-8"?><tasks><insertContact><contact uid="0"><salutation>Mr.</salutation><familyName>Smith</familyName><givenName>John</givenName><organisationName></organisationName></contact></insertContact></tasks>';
$cmd = 'curl -X '.' "'.$xml.'" '.$web_service;
exec($cmd, $result);
print_r($result); // Getting Null
Method-2
$curlData = '<?xml version="1.0" encoding="utf-8"?><tasks><insertContact><contact uid="0"><salutation>Mr.</salutation><familyName>Smith</familyName><givenName>John</givenName><organisationName></organisationName></contact></insertContact></tasks>';
$url = https://secure.saasu.com/webservices/rest/r1/Tasks?wsaccesskey=XXXXXXXXXXXXXXXXX&FileUid=XXX';
$curl = curl_init();
curl_setopt ($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl,CURLOPT_HTTPHEADER,array (
'SOAPAction:""',
'Content-Type: text/xml; charset=utf-8',
));
curl_setopt ($curl, CURLOPT_POST, 1);
curl_setopt ($curl, CURLOPT_POSTFIELDS, $curlData);
$result = curl_exec($curl);
curl_close ($curl);
print_r($result); //NULL RESULT
Saasu help : http://help.saasu.com/api/
Finally, got the response. Here is the code
$curlData = '<?xml version="1.0" encoding="utf-8"?><tasks><insertContact><contact uid="0"><salutation>Mr.</salutation><familyName>Smith</familyName><givenName>John</givenName><organisationName></organisationName></contact></insertContact></tasks>';
$url = https://secure.saasu.com/webservices/rest/r1/Tasks?wsaccesskey=XXXXXXXXXXXXXXXXX&FileUid=XXX';
$curl = curl_init();
curl_setopt ($curl, CURLOPT_URL, $url);
//curl_setopt ($curl, CURLOPT_URL, "http://www.php.net");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_VERBOSE, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt ($curl, CURLOPT_POST, 1);
curl_setopt ($curl, CURLOPT_POSTFIELDS, $curlData);
$curl_response = curl_exec ($curl);
curl_close ($curl);
$xml = new SimpleXMLElement($curl_response);
print_r($xml);
Thanks

Categories