$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://itunes.apple.com/search?term=Clean%20Bandit%20-%20Rather%20Be&entity=song&limit=10&lang=fr_fr');
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
if(curl_errno($ch))
echo 'Curl error: '.curl_error($ch);
$CurResult = curl_exec($ch);
curl_close($ch);
echo 'Result:'.$CurResult;
$url = 'https://itunes.apple.com/search?term=Clean%20Bandit%20-%20Rather%20Be&entity=song&limit=10&lang=fr_fr';
$content = file_get_contents($url);
print_r($content);
Use this code to get the response curl is not needed in this case
from php manual
curl_errno()
does not return true or false, it returns error number 0, if no errors.
so you either change the condition to
if(curl_errno($ch)!=0)
or use curl_error()
if(curl_error($ch)!=''){
echo "error: ".curl_error($ch);
}
http://se2.php.net/manual/en/function.curl-errno.php
Related
funtion request(){
echo "Success";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_TIMEOUT, $request_timeout);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $request_timeout);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($ch);
$curl_error = curl_errno($ch);
$getserver = curl_getinfo($ch);
curl_close($ch);
}
here success echo only shows after exicuting curl function . i wanna
echo the success message before curl function calls.
Remove the Curl Function, Make sure it's running the echo first. I can't comment.
If this code is in the controller and you are calling from a different function in the same file, you need to call $this->request(), let me convey this information.
Later,
maybe you can return a result with return "Success";
I try to get the content of this website with cURL
www.mytischtennis.de/public/
but it gets no body response. With many other websites the code works:
<?php
$output = grabPage(
"http://www.mytischtennis.de/public/"
//"http://www.spiegel.de" //this page and many other pages are working
);
if (is_array($output)) {
var_dump($output);
} else {
echo $output;
}
function grabPage($url)
{
$ch = curl_init();
$cookiePath= dirname(__FILE__) . "\cookie.txt";
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_MAXREDIRS, 50);
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
curl_setopt($ch, CURLOPT_TIMEOUT, 40);
curl_setopt($ch, CURLOPT_COOKIE, 'CFID=c7a592d8-5798-4471-9af4-4c4d954d03cd; cfid=c7a592d8-5798-4471-9af4-4c4d954d03cd; MYTT_COOKIESOK=1; CFTOKEN0=; cftoken=0; SRV=74');
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookiePath);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookiePath);
$fpErrors = fopen(dirname(__FILE__) . '\errorlog.txt', 'w');
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_STDERR, $fpErrors);
curl_setopt($ch, CURLOPT_URL, $url);
ob_start();
$curl_exec = curl_exec($ch);
ob_end_clean();
if ($curl_exec === false) {
echo 'Error: ' . curl_error($ch);
} else {
echo 'Success';
}
var_dump(curl_getinfo($ch));
curl_close($ch);
return $curl_exec;
}
I tried to read a fiddler/wireshark dump of a browser request to this website. But I can't figure out which of that many requests and which parameters are necessary to get the content.
You can test cURL with the url www.mytischtennis.de/public/ also on this website:
http://onlinecurl.com/
You need to accept gzip encoding in the response by sending the appropriate HTTP header in the request:
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept-Encoding: gzip'));
Now your answer from the server might or might not be gziped. The proper way to check that is to interpret the Content-Encoding HTTP header in the response. But you can also do it quick and dirty like this:
$content = #gzdecode($curl_exec);
return $content !== false ? $content : $curl_exec;
No error, it just show a blank screen, I have no idea where has gone wrong in myt case.
$username = 'myname';
$password = 'mypass';
$loginUrl = 'https://example.com/login/';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $loginUrl);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, 'user='.$username.'&pass='.$password);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$content = curl_exec($ch);
echo $content;
add this option in case of url with HTTPS and see if you can get some information:
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
Try to check for curl errors.
http://php.net/manual/en/function.curl-error.php
if(curl_exec($ch) === false)
{
echo 'Curl error: ' . curl_error($ch);
}
else
{
echo 'Operation completed without any errors';
}
If curl_exec returns false. Echo of the contents returns nothing.
There may be some errors but if error reporting is disabled on the server side you might not just see it.
You can check the returning http headers, they might give you some ideas.
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"https://test.com");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, 'x=32423');
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
if(curl_exec($ch) === false)
{
echo 'Curl error: ' . curl_error($ch);
}
else
{
'OK';
}
This is what outputted,when i run this page
access_token=AAAdsfsdfds32432fadfcazdfadsfadsfdas
How do i extract this and pass it a variable?
There is a typo in your postfields. The postfields should be as follows:
curl_setopt($ch, CURLOPT_POSTFIELDS, array('x'=>'32423'));
instead of:
curl_setopt($ch, CURLOPT_POSTFIELDS, 'x=32423'');
First off, you need to change your CURLOPT_HEADERS to true, and you need
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
and
$result=curl_exec($ch)
if( $result=== false)
Then, according to an answer I saw elsewhere on SO, this should get you the headers:
list($headers,$content) = explode("\r\n\r\n",$result,2);
foreach (explode("\r\n",$headers) as $hdr)
print_r($hdr); //see what it gives you and then edit this accordingly.
echo $content;
Sounds like you just want
$token = end(explode('=', $access_token_string));
I'm doing curl post but the problem is that I need to return empty results so I can echo out another value for my ajax request results:
below is the code:
$ch = curl_init("http://www.rankreport.com.au/ajax/add_new_lead");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "lead_company_id=1&lead_business=1234&lead_first_name=asdf&lead_website=12314.com&lead_phone=1234&lead_email=test#test.com&lead_package=seo");
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_exec($ch);
curl_close($ch);
echo 'error 3';
When I do this, I'm getting below results:
{"lead_date":1315637343,"lead_company_id":"1","lead_business":"1234","lead_first_name":"asdf","lead_last_name":"","lead_website":"12314.com","lead_phone":"1234","lead_email":"test#test.com","lead_package":"seo"} 3
How do set curl options so that it doesn't return any curl results?
Set CURLOPT_RETURNTRANSFER:
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$result = curl_exec($ch);
Use CURLOPT_RETURNTRANSFER = true to have the returned content as a return value ($ch) instead of output.