PHP How can I open several sources using curl? - php

I have some code to get json content of a site1 but I also need to get content of a site2. Should I rewrite all these lines again for the site2? Or maybe I can add one more URL in the curl_setopt?
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"http://site1.com");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
$outputJson = curl_exec($ch);
if ($outputJson === FALSE) {
echo 'Sorry, This service is currently unavailable: '. curl_error($ch);
}

You can create a function like
function get_data($url)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
$outputJson = curl_exec($ch);
if ($outputJson === FALSE) {
echo 'Sorry, This service is currently unavailable: '. curl_error($ch);
}
return $outputJson;
}
and call it with
get_data("http://blah.com");
get_data("http://blah1.com");
This might not be an optimal solution but shuould work for simple instances

You can get better performance with multi url curl.
See :
http://php.net/manual/en/function.curl-multi-exec.php
And :
http://www.rustyrazorblade.com/2008/02/curl_multi_exec/

You might want to try to loop trought the different site:
$aSites = array("http://site1.com","http://site2.com");
for($x=0; $x<count($aSites); $x++){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$aSites[$x]);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
$outputJson = curl_exec($ch);
if ($outputJson === FALSE) {
echo 'Sorry, This service is currently unavailable: '. curl_error($ch);
}
}

<?
$url1 = "http://site1.com";
$url2 = "http://site2.com";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
$outputJson = curl_exec($ch);
curl_setopt($ch, CURLOPT_URL, $url2);
$outputJson2 = curl_exec($ch);
curl_close($ch);
if ($outputJson === FALSE || $outputJson2 === FALSE) {
echo 'Sorry, This service is currently unavailable: '. curl_error($ch);
}
?>

Related

Playing .m3u8 hls using php curl

I'm attempting to play a .m3u8 video using a PHP curl proxy. The following code doesn't work:
<?php
//http://127.0.0.1/m3u8.php
//....proxy info
$auth = 'username:password';
$proxy_ip = '1.2.3.4.5';
$proxy_port = 8080;
$path = $_GET['link'];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $path);
//curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_PROXYPORT, $proxy_port);
curl_setopt($ch, CURLOPT_PROXYTYPE, 'HTTP');
curl_setopt($ch, CURLOPT_PROXY, $proxy_ip);
curl_setopt($ch, CURLOPT_PROXYUSERPWD, $auth);
curl_exec($ch);
if (curl_error($ch)) {
$error_msg = curl_error($ch);
echo $error_msg;
}
curl_close($ch);
if (isset($error_msg)) {
echo $error_msg;
}
?>
Page returns "malformed malformed" error, any ideas to solve this problem?
Thanks

How can I check if RESTAPI is down using curl php

I wants to make a virtual environment to check if web-service is down.
Case: If web-service is down for 7 seconds then system should re-try once and if not work then it should send the notification to admin.
$ch = curl_init();
curl_setopt($ch, CURLOPT_FAILONERROR, true);
curl_setopt($ch, CURLOPT_URL, $url);
// Use 0 to wait indefinitely.
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 7);
// The maximum number of seconds to allow cURL functions to execute
curl_setopt($ch, CURLOPT_TIMEOUT, 7);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
//curl_setopt($ch, CURLOPT_NOBODY, TRUE); // remove body
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$responseData = curl_exec($ch);
$curl_errno = curl_errno($ch);
$curl_error = curl_error($ch);
$httpCode = curl_getinfo($ch);
curl_close($ch);
You could try this:
$url = 'my.url';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_exec($ch);
$retcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($retcode == 200)
{
// It's working
}
else
{
// It's down
}
Finally, I found the solution.
function checkData($loop = 0){
$url = 'http://google.co.in:8080/';
$ch = curl_init();
curl_setopt($ch, CURLOPT_FAILONERROR, true);
curl_setopt($ch, CURLOPT_URL, $url);
// Use 0 to wait indefinitely.
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 7);
// The maximum number of seconds to allow cURL functions to execute
curl_setopt($ch, CURLOPT_TIMEOUT, 7);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
//curl_setopt($ch, CURLOPT_NOBODY, TRUE); // remove body
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$responseData = curl_exec($ch);
$curl_errno = curl_errno($ch);
$curl_error = curl_error($ch);
$httpCode = curl_getinfo($ch);
curl_close($ch);
if ($curl_errno > 0){
if($loop > 1 ){
//send mail to admin
}else{
$loop++;
sleep(7);
checkData();
}
}
}
checkData();

php curl scrape not working

My scraping code is not working , I used php cURL , But not returning page content . So please help me , how can I get all content .
function getSslPage($url)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_REFERER, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
$url = "https://www.woolworths.com.au/Shop/SearchProducts?search=Fresh%20Apple";
$ch = getSslPage($url);
echo $ch;
Add this right after your curl_exec() call and copy/paste here the output:
if (curl_errno($ch)) {
throw new Exception(curl_error($ch));
}

php curl request returns nothing

i am using codeigniter and my code is as follows :
Controller :
$json_string = 'http://maps.googleapis.com/maps/api/directions/json?origin=' . $user_addr . '&destination=' . $vendor_city .'&mode=Montreal';
$json = $this->curl_get_contents($json_string);
$obj = json_decode($json, true);
$this->load->view('get_direction_pg',$obj);
public function curl_get_contents($url)
{
$ch = curl_init($url);
if ($ch == FALSE) {
return array('error' =>"failed");
}else{
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_VERBOSE, 0);
$result = curl_exec($ch);
return $result;
}
}
and it if i access var_dump($routes);
it says,
undefined variable routes.
try using this: i.e. encoding the vars - before putting into url
$json_string = 'http://maps.googleapis.com/maps/api/directions/json?origin=' . urlencode($user_addr) . '&destination=' . urlencode($vendor_city) .'&mode=Montreal';
the parameters should be encoded for url, otherwise it is being invalid, and hence you are getting that error
Try this:
public function curl_get_contents($url)
{
$ch = curl_init($url);
if ($ch == FALSE) {
return array('error' =>"failed");
}
else
{
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER,0);
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER["HTTP_USER_AGENT"]);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_VERBOSE, 0);
$result = curl_exec($ch);
return $result;
}
}

cURL not bringing back values

I am using cURL through a PHP file to create a fogbugz case using values I will pull from the current ticket. Right now I am only trying to display the token received when a user logs into fogbugz and I am not getting any values from the response.
Here is the code I am using in my PHP file:
<?php
$user = "USERNAME";
$password = "PASSWORD";
$url = "fogbugz.spllc.local/api.asp?";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 15);
curl_setopt($ch, CURLOPT_PROXYUSERPWD, "[$user]:[$password]");
if (false === ($output = curl_exec($ch)))
{
print "error" . curl_error($ch);
die("ERROR" . curl_error($ch));
}
else if (200 !== (int)curl_getinfo($ch, CURLINFO_HTTP_CODE))
{
print "200";
die("200");
}
print curl_exec($ch);
curl_close($ch);
?>
Can someone help point me in the right direction?
Thanks
When you read the documentation on http://fogbugz.stackexchange.com/fogbugz-xml-api you will find you have to send a get or post request.
Something like shown below should work:
$ch = curl_init('http://fogbugz.spllc.local/api.asp?cmd=logon&email=John%20Hancock&password=BigMac');
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($ch);
curl_close($ch);
also take a look at: https://github.com/there4/fogbugz-php-api

Categories