Curl automatically display the result? - php

I'm using php 5.3.2 and when i execute a curl it display the result directly without adding a print or echo function.
Here is my code:
<?php
$pvars = array('query' => 'ice age', 'orderby' => 'popularity');
$timeout = 30;
$myurl = "http://www.website.com";
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $myurl);
curl_setopt($curl, CURLOPT_TIMEOUT, $timeout);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $pvars);
$xml = curl_exec($curl);
curl_close ($curl);
?>
What's wrong with my code and why it displays the result?

By default, the curl extension prints out the result.
You need to enable the CURLOPT_RETURNTRANSFER option, like so:
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
After that option is enabled, curl_exec will return the result, instead.

after php 5.1 curl always displays results as you can view on the documention. to avoid that simply do:
echo "< span style='display:none'>";
$pvars = array('query' => 'ice age', 'orderby' => 'popularity');
$timeout = 30;
$myurl = "http://www.website.com";
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $myurl);
curl_setopt($curl, CURLOPT_TIMEOUT, $timeout);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $pvars);
$xml = curl_exec($curl);
curl_close ($curl);
echo"< /span>";

Related

Update data using PUT with CURL PHP

I m try to update some data on mongodb using cms api
on terminal i can update information like this
curl -X PUT -d name=12345a https://api1.MYWEBISITE.com/api/v1in/user/2039/?t=mytoken
now using PHP i have try so many ways and no one looks to work for me
tried like this
class Curl {
public function put($url, $data_string){
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_FAILONERROR, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data_string))
);
$result = curl_exec($ch);
return $result;
}
}
$curl = new Curl;
$data_string = '{"name" : "name123"}';
$url = "https://api1.MYWEBSITE.com/api/v1in/user/2039/?t=mytoken";
echo $curl->put($url, $data_string);
also i tried like this
$data = array( "name" => '12344');
$url = "https://api1.mywebsite.com/api/v1in/user/2039/?t=mytoken";
$curl = curl_init($url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
$result = curl_exec($curl);
$response = json_decode($result);
var_dump($response);
curl_close($curl);
both php solutions dont works, nothing is updated, no error msg is showing
any help?

Why curl returns 1 although parametre CURLOPT_RETURNTRANSFER is set

this is the servers array that I keep top level domain names and extensions
protected $servers = array(
"com.tr" => "whois.nic.tr",
"gen.tr" => "whois.nic.tr",
"web.tr" => "whois.nic.tr",
"k12.tr" => "whois.nic.tr",
"org.tr" => "whois.nic.tr"
);
I am required to get whois info so I wrote this function
function whois($domain,$ext)
{
$server=$this->servers[$ext];
$domain=$domain.".".$ext;
if (function_exists('curl_version')) {
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $server);
curl_setopt($curl, CURLOPT_PORT, 43);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_TIMEOUT, 5);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $domain."\r\n");
$result = curl_exec($curl);
curl_close($curl);
echo '<pre>';
echo print_r($result);
echo '</pre>';
} else {
trigger_error('cURL is not found!');
exit();
}
}
I am using this way
whois("google","com");
This function works for google.com and It gives me the correct domain information But when I execute for google.org.tr then (this is the intersting part) sometimes retuns
No match found for "google.org.tr"(whis is expected)
and somethimes return "1"
#update
curl_setopt($curl, CURLOPT_URL, $server);
curl_setopt($curl, CURLOPT_URL, "telnet://whois.web.tr:43");
curl_setopt($curl, CURLOPT_PORT, 43);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $domain."\r\n");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($curl, CURLOPT_NOPROGRESS, TRUE);
curl_setopt($curl, CURLOPT_PROTOCOLS, CURLPROTO_TELNET);

PHP code on curl

THis code is not showing up the google home page. Please point out the error in it.
<?php
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, "https://www.google.com.kw");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($curl);
curl_close($curl);
print $result;
?>
Remember that if your response is string-based; curl automatically encodes the response in utf8. So it might be necessary to decode the String to get what you want. Otherwise, your code is OK but here is another variant:
<?php
$serviceURL = "https://www.google.com.kw";
$curl = curl_init();
$settings = array(
CURLOPT_URL => $serviceURL,
CURLOPT_POST => false,
CURLOPT_RETURNTRANSFER => true,
);
curl_setopt_array($curl, $settings);
$response = curl_exec($curl);
$response = utf8_decode ( $response ); // <== DECODES THE UTF8 ENCODED STRING.
if(curl_errno($curl)){
var_dump( curl_error($curl) );
exit;
}
print($response);
curl_close($curl);
Try This :
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, "https://www.google.co.in");
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: text/plain'));
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Accept-Encoding: gzip,deflate'));
curl_setopt($curl,CURLOPT_ENCODING, 1);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_HEADER, 1);
$result = curl_exec($curl);
print $result;

How to get JSON data from Rest API by PHP Curl?

I have a Rest api which I can access by this url: "http://127.0.0.1:8000/api/thesis/?format=json". Now I want to get the JSON data from it. For connecting to the api I tried to use PHP-Curl as below. But I get NULL! (This is the first time I'm doing php any help will be great!)
<?php
$service_url = "http://127.0.0.1:8000/api/thesis/?format=json";
//initialize a curl session
$curl = curl_init();
//set options for the transfer
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_TIMEOUT, 5);
curl_setopt($curl, CURLOPT_URL, $service_url);
curl_setopt($curl, CURLOPT_POST, false);
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-type:application/json'));
//execute the session
$curl_response = curl_exec($curl);
//finish off the session
curl_close($curl);
$curl_jason = var_dump(json_decode($curl_response, true));
print_r($curl_jason);
echo $curl_jason;
?>
I think you should use GET method of curl like this
$service_url = "http://127.0.0.1:8000/api/thesis/?format=json";
$curl = curl_init($service_url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
//execute the session
$curl_response = curl_exec($curl);
//finish off the session
curl_close($curl);
$curl_jason = json_decode($curl_response, true);
print_r($curl_jason);
Use the below mentioned code snippet to fetch data from a REST API using PHP curl
<?php
function _isCurl(){
return function_exists('curl_version');
}
if (_iscurl()){
//curl is enabled
$url = "http://testDomainName/restAPI.php?id=123&amt=100&jsonp=?";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec($ch);
curl_close($ch);
print_r($output);
// Curl operations finished
}
else{
echo "CURL is disabled";
}
?>

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);

Categories