I am trying to make an ajax call to a php script. The php script calls an rss feed using curl, gets the data, and returns the data to the funciton.
I keep getting an error
"Warning: Wrong parameter count for curl_error() in" ....
Here is my php code:1
$ch = curl_init() or die(curl_error());
curl_setopt($ch, CURLOPT_URL, $feed);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$data1 = curl_exec($ch) or die(curl_error());
echo $data1;
and the ajax call:
$.ajax({
url: "getSingleFeed.php",
type: "POST",
data: "feedURL=" + window.feedURL,
success: function(feed){
alert(feed);
}});
I tested all the variables, they are being passed correctly, I can echo them out. But this line: $data1 = curl_exec($ch) or die(curl_error());
is what is giving me the error. I am doing the same thing with curl on other pages, just without ajax, and it is working fine. Is there anything special I need to do with ajax to do this?
The PHP manual states that the first parameter of curl_error is a handle returned by curl_init();. Try replacing what you pasted with what is given below. This adds the $ch variable as a parameter to all the instances of curl_error();.
$ch = curl_init() or die(curl_error($ch));
curl_setopt($ch, CURLOPT_URL, $feed);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$data1 = curl_exec($ch) or die(curl_error($ch));
echo $data1;
Related
I have a PHP Code, which reloads itself with another GET value. Like: example.com?number=453 and it keeps doing this for days. I was doing this in the browser. But i found cron job is way better.
So, I need to use CURL to reload the page with a new GET value like ?number=550. So this is the code, which i use (found it on stackoverflow)
function curl_download($Url){
// is cURL installed yet?
if (!function_exists('curl_init')){
die('Sorry cURL is not installed!');
}
// OK cool - then let's create a new cURL resource handle
$ch = curl_init();
// Now set some options (most are optional)
// Set URL to download
curl_setopt($ch, CURLOPT_URL, $Url);
// Set a referer
curl_setopt($ch, CURLOPT_REFERER, "http://www.example.org/yay.htm");
// User agent
curl_setopt($ch, CURLOPT_USERAGENT, "MozillaXYZ/1.0");
// Include header in result? (0 = yes, 1 = no)
curl_setopt($ch, CURLOPT_HEADER, 0);
// Should cURL return or print out the data? (true = return, false = print)
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Timeout in seconds
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
// Download the given URL, and return output
$output = curl_exec($ch);
// Close the cURL resource, and free system resources
curl_close($ch);
return $output;
}
$output = curl_download("http://www.example.com/yourscript.php?number='$requestsDone'");
And at the end of the page, i use this, to call the function
curl_download($Url);
But i am getting this error:
Notice: Undefined variable: Url
in the last line, i.e curl_download($Url);
This is how we call the function right? What's wrong? Also is there any mistake or improvement, I can make in the code?
You not assign values to variables $Url.
Before you can assign values to variables $Url. That like $Url = "http://domaintest.com/?number=550";
curl_download($Url);
I am trying to use curl with PHP to execute a POST call from PHP but always I get page error
this is my code
if($curl = curl_init()){
curl_setopt($curl, CURLOPT_URL, 'http://marcelotavarez.com/freeodds.aspx');
curl_setopt($curl, CURLOPT_RETURNTRANSFER,true);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, "CallbackMethod=GetLines&Parm1=20170110&Parm2=2&Parm3=0&CallbackParmCount=3&__WW EVENTCALLBACK=CustomerCallback&__WWEVENTTARGET=Page&__VIEWSTATE=/wEPDwUKLTQwNjM5NzY1NmRkOa+UqFoVFF+BzUdMUBBH1ahGsWQ=&__VIEWSTATEGENERATOR=BB49A8FA&");
$out = curl_exec($curl);
echo "===\n $out \n====\n";
$out = str_replace("\"","",$out);
}
Using the inspector from Chrome I checked the header that I need to use to do the POST Call and these are:
CallbackMethod:GetLines
Parm1:"20170110"
Parm2:2
Parm3:0
CallbackParmCount:3
__WWEVENTCALLBACK:CustomerCallback
__WWEVENTTARGET:Page
__VIEWSTATE:/wEPDwUKLTQwNjM5NzY1NmRkOa+UqFoVFF+BzUdMUBBH1ahGsWQ=
__VIEWSTATEGENERATOR:BB49A8FA
Any time that I use this code just I get the error page
Any cluees or any help , pls
TIA
I need to ask you a question where is CallbackMethod=GetLines
GetLines function , I think must be in your code to be executed after response back
I need a function that return me the Timezone of a specific location, so i use the
Google Time Zone API.
function timezoneLookup($lat, $lng){
$url = 'https://maps.googleapis.com/maps/api/timezone/json?location='.$lat.','.$lng.'×tamp='.time().'&sensor=false';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec($ch);
curl_close($ch);
return $output;
}
The function doesn't work because if i return $url i can see that GET variable "×tamp=" is transformed into "×tamp=".
If i run the script outside the function it works.
WHY??
----UPDATE----
I resolved the problem, the curl doesn't work with https://, so i add:
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
See this for more information PHP cURL Not Working with HTTPS
The function works fine. The reason you're seeing ×tamp= is because × is being converted to ×. If you view source you'll see the correct url(instead of viewing the converted entity on the web page).
Why ; is not required
There is no problem with this function. If you echo that URL you will get the multiplication sign because it is being filtered through html and recognizing the ascii code. This only happens when you view it though and html viewer (browser), if you view source you will see the original string.
To confirm that this conversion will not occur when passed through curl_setopt(), I ran your code on my server and got an expected result.
echo timezoneLookup(52.2023913, 33.2023913);
function timezoneLookup($lat, $lng){
$url = 'https://maps.googleapis.com/maps/api/timezone/json?location='.$lat.','.$lng.'×tamp='.time().'&sensor=false';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec($ch);
curl_close($ch);
return $output;
}
Returned...
{ "dstOffset" : 3600, "rawOffset" : 7200, "status" : "OK", "timeZoneId" : "Europe/Kiev", "timeZoneName" : "Eastern European Summer Time" }
If this code is not working for you then it could be a networking issue. Try doing curl with another webpage and see what happens. Also, with a simple api call like this you could easily use file_get_contents()
I am usig curl_setopt to eval another file from a different server, the link is something like this "http://somewhere.com/index.php?vars=hello"
now in somewhere.com/index.php i need to get the value of vars that was passed using curl, but so far i cant get any values at all.
here is my sample code for your reference this is from the file calling somewhere.com:
$d = "http://somewhere.com/index.php?vars=hello";
$ch1 = curl_init();
curl_setopt($ch1, CURLOPT_AUTOREFERER, TRUE);
curl_setopt($ch1, CURLOPT_HEADER, 0);
curl_setopt($ch1, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch1, CURLOPT_URL, $d);
curl_setopt($ch1, CURLOPT_FOLLOWLOCATION, TRUE);
$data1 = curl_exec($ch1);
eval($data1);
curl_close($ch1);
in somewhere.com/index.php i already did print_r($_GET); to view any passed values to the file but it returned nothing.
What is happening in the index.php file with the variable "hello"? When you do a curl post to another page that other page typically will echo out a response and that response is what you evaluate. Can you post your code from index.php so we can see what you are trying to do?
Edit: Also you can use chrome inspector, fiddler or some other network monitor to see if the http request actually is being fired off and to check that you are actually getting a 200 response back.
Edit: I don't know what you are using eval either, just echo the response. If you have an output buffer issue then start buffering before you echo the response like:
ob_start():
echo $data1;
ob_end_clean();
Also if you want to see if you are getting any errors just do this:
if(curl_exec($ch1) === false)
{
echo 'Curl error: ' . curl_error($ch1);
}
else
{
echo $data1;
}
// Close handle
curl_close($ch1);
can't you just use $_GET[]?
i.e.
$variables = $_GET['vars'];
Hi I'm a little new at CURL, but I'm trying to request some json data and then parse the results. I am having success with retrieving the data, but I can't handle the response. Here's the code
function bitBucketCurl($url)
{
global $bitPassword;
global $bitUsername;
$ch = curl_init();
// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_USERPWD, "$bitUsername:$bitPassword");
curl_setopt($ch, CURLOPT_HEADER, 0);
// grab URL and pass it to the browser
$commitinfo = curl_exec($ch);
// close cURL resource, and free up system resources
curl_close($ch);
return $commitinfo;
}
$json = bitBucketCurl($url);
echo $json; // This seems to work in that, when I load the page, I can see the json data
//turn json data into an array - this is what does not seem to be working
$obj_a = json_decode($json, true);
print_r ($obj_a); //the result is simply a 1 rather than the array I would expect
The basic problem is the json data shows up when I echo $json but when I try to turn that data into an array it doesn't work. When I print the array, I just get a '1'.
I got the required result by adding the following line:
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);