I have a curl call I want to run but once it runs, I would like to run another to a different url but with the same options.
Can I run another call without having to copy and paste the options, I need to run about 5 calls, it seems like there is a way to accomplish this. I cannot run them all at the same time, I need to make sure that I get the result from one, then if certain critera is met, I need to run another one.
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
$result = curl_exec($ch);
curl_close($ch);
Simply update the url (using the CURLOPT_URL option) before each additional request. See the comments in the below example.
// initialize with the first url you want to use
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
$result = curl_exec($ch);
// check the result of the first request
if($result == "the content you want")
{
// if the result dictates that you make another request, update the url
curl_setopt($ch, CURLOPT_URL, $url2);
// execute the second request
$result2 = curl_exec($ch);
// do something with $result2
}
// only close curl after you are done making your requests
curl_close($ch);
If I haven't understood wrongly.
Yes, I always use a way to run 1 cURL action to more than 1 URL.
And you should use:
EDIT: Method 2 isn't working.
Method 1:
<?php
// Arrays
$ch=array();
$url=array();
$result=array();
// ************
$url['1'] = 'http://url1.com';
$url['2'] = 'http://url2.com';
$ch['1'] = curl_init($url['1']);
curl_setopt($ch['1'], CURLOPT_HEADER, true);
curl_setopt($ch['1'], CURLOPT_NOBODY, true);
curl_setopt($ch['1'], CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch['1'], CURLOPT_TIMEOUT, 10);
$result['1'] = curl_exec($ch['1']);
curl_close($ch['1']);
$ch['2'] = curl_init($url['2']);
curl_setopt($ch['2'], CURLOPT_HEADER, true);
curl_setopt($ch['2'], CURLOPT_NOBODY, true);
curl_setopt($ch['2'], CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch['2'], CURLOPT_TIMEOUT, 10);
$result['2'] = curl_exec($ch['2']);
curl_close($ch['2'])
?>
Method 2
<?php
$url = array(
'http://url1.com',
'http://url2.com'
);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
$result = curl_exec($ch);
curl_close($ch)
?>
In method 1 we used arrays in $ch and $url and $result
In method 2 we made array of $url
Related
I have a curl set up like this:
function file_get_contents_curl($url)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_TIMEOUT, 400);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch,CURLOPT_BINARYTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
$data = curl_exec($ch);
return $data;
}
When I call it with the following:
$html = file_get_contents_curl("https://training.sap.com/service-id/module.php/core/as_login.php?AuthId=sf-sp&ReturnTo=%2Fsuccessfactors-community%2Fpermission-check");
You can see that link has an authentication, and I do not want to spend time on it. I just want that curl session to die after maybe 3 seconds or x seconds, or determine before hand if it is going to be a never ending loop on the curl for one reason such a SSL or password requirement etc.
I'm making two seperate cURL requests in the same php page. When I load the page by itself, it works as expected, with each request returning different, correct data.
However, when I load the page via AJAX, the second request shows the same data as the first. Why is this happening? Code follows below:
$auth = base64_encode( 'user:'.$api_key );
$data = array(
'apikey' => $api_key,
);
$json_data = json_encode($data);
$ch = curl_init();
$ch2 = curl_init();
$curlopt_url = "https://us7.api.mailchimp.com/3.0/reports/".$_GET['id'];
curl_setopt($ch, CURLOPT_URL, $curlopt_url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json','Authorization: Basic '.$auth));
curl_setopt($ch, CURLOPT_USERAGENT, 'PHP-MCAPI/3.0');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, $json_data);
$result = curl_exec($ch);
$results = json_decode($result, true); ?>
$curlopt_url_b = "https://us7.api.mailchimp.com/3.0/reports/".$_GET['id'].'/sent-to/?count=5000 ';
curl_setopt($ch2, CURLOPT_URL, $curlopt_url_b);
curl_setopt($ch2, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($ch2, CURLOPT_HTTPHEADER, array('Content-Type: application/json','Authorization: Basic '.$auth));
curl_setopt($ch2, CURLOPT_USERAGENT, 'PHP-MCAPI/3.0');
curl_setopt($ch2, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch2, CURLOPT_TIMEOUT, 10);
curl_setopt($ch2, CURLOPT_POST, true);
curl_setopt($ch2, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch2, CURLOPT_POSTFIELDS, $json_data);
$recipient_result = curl_exec($ch2);
$recipients = json_decode($recipient_result, true);
When loaded via AJAX, $recipients == $results, when they should return results from completely different end points. What gives?
The problem was with the AJAX request, not the cURL request. The AJAX request had added an additional paramter to the URL (in this case, 'ajax=true'), which was feeding through into $_GET variable, and hence requesting the wrong endpoint. It loaded correctly when loaded independently because the URL was not changed by the JS.
Is it possible to split curl in 3 parts?
For example, I have the following code:
$headers = array(
"Content-Type:application/json",
"Authorization:key=" . self::ANDROID_AUTH_KEY
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://android.googleapis.com/gcm/send");
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
$result = curl_exec($ch);
//result sample {"multicast_id":6375780939476727795,"success":1,"failure":0,"canonical_ids":0,"results":[{"message_id":"0:1390531659626943%6cd617fcf9fd7ecd"}]}
//http://developer.android.com/google/gcm/http.html // refer error code
curl_close($ch);
In this case, I would like to have a method, for example : connect() that will do curl_init() once, after that I want to have a method send() that will call curl_exec() which then posts parameters 10x times, and after that disconnect() that will do curl_close() that will be called once.
Is this something which is possible?
Please help me.
I want to pull some html pice of code with some data from an external website with cURL, but a recived a null response. If I type the url in browser i get the data that i want but i need to do that from my app via cURL or file_get_contents();
This is my code :
$_awb = '888000074599';
$url = 'http://89.45.196.45';
$post_fields = ':8080/?id=8IM*8J*9K&NT='.$_awb;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_fields);
$result = curl_exec($ch);
curl_close($ch);
You should not be using CURLOPT_POSTFIELDS. You are trying to do a GET request with a query string, but postfields is intended for making POST requests.
So setting CURLOPT_URL to the proper (absolute) URL should work.
$url = "http://89.45.196.45:8080/?id=8IM*8J*9K&NT="
$nt = "888000074599";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url . $nt);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($ch);
curl_close ($ch);
I am looking to call the Twitter API to grab tweets (successfully achieved tweets on load) but now I am looking to update the page automatically, allowing the tweets to automatically load without reload/user interaction.
I know this type of functionality is possible (monitter.com) does it, but what is the technology used to do so? Can I do it with PHP?
Thanks
As #suresh.g said, you can use AJAX. The simplest way: use jQuery.
Also, you can use an iframe that reloads every 10 seconds with the setInterval() javascript function. The user will not have a reload of his entire page, but the twitter iframe.
Another type of technology is COMET or PUSH technology, but I don't think you need it right now, but it's good to know about it ;)
use curl
function curl_grab_page($url,$data,$secure="false",$ref_url="",$login = "false",$proxy = "null",$proxystatus = "false")
{
if($login == 'true') {
$fp = fopen("cookie.txt", "w");
fclose($fp);
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_COOKIEJAR, "cookie.txt");
curl_setopt($ch, CURLOPT_COOKIEFILE, "cookie.txt");
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
if ($proxystatus == 'true') {
curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, TRUE);
curl_setopt($ch, CURLOPT_PROXY, $proxy);
}
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
if($secure=='true')
{
curl_setopt($ch, CURLOPT_SSLVERSION,3);
}
curl_setopt( $ch, CURLOPT_HTTPHEADER, array( 'Expect:' ) );
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_REFERER, $ref_url);
curl_setopt($ch, CURLOPT_HEADER, TRUE);
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
ob_start();
return curl_exec ($ch); // execute the curl command
curl_getinfo($ch);
ob_end_clean();
curl_close ($ch);
unset($ch);
}
just call this function the way you want the data you want to set it can do every thing for you just dont forget to setup curl in you php.ini
thanks