What is the best approch using CURLS in php? - php

I have the following code wherein I am calling a data through php CURL.
$URL = '//abc.com';
$gb = curl_init();
curl_setopt($gb,CURLOPT_URL,$URL);
curl_setopt($gb,CURLOPT_RETURNTRANSFER,1);
curl_setopt($gb,CURLOPT_CONNECTTIMEOUT,10);
curl_setopt($gb,CURLOPT_TIMEOUT,10);
curl_setopt($gb,CURLOPT_SSL_VERIFYPEER,false);
$res = curl_exec($gb);
curl_close($gb);
$data = json_decode($res,true);
What is the best way to call CURL request in case I have multiple variants of URL like?
1). //abc.com
2). //abc.com/abc
3). //abc.com/123
Should I call CURL multiple times or how to define it in php function?

You can do something like
function curlRequest($url){
$gb = curl_init();
curl_setopt($gb,CURLOPT_URL,$url);
curl_setopt($gb,CURLOPT_RETURNTRANSFER,1);
curl_setopt($gb,CURLOPT_CONNECTTIMEOUT,10);
curl_setopt($gb,CURLOPT_TIMEOUT,10);
curl_setopt($gb,CURLOPT_SSL_VERIFYPEER,false);
$res = curl_exec($gb);
$data = json_decode($res,true);
return $data;
}
$urls = ["http://url1","http://url2","http://url3"];
foreach($urls as $url){
curlRequest($url);//do something with data
}
I don't see difference calling the same domain or other since different routes retrieve different information. If all of these urls are equivalents, you don't need foreach or for solution.

It depends.
But if you connect to same website / service every time, you can freely use same connection.
This will allow you to set parameters for the connection one time only (eg. cookies or other headers).
You may also extract CURL handler creation to separated function and then only switch URLs for specific requests.
Your code should look like:
function init_my_curl() {
$h = curl_init();
curl_setopt($h, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($h, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($h, CURLOPT_TIMEOUT, 10);
curl_setopt($h, CURLOPT_SSL_VERIFYPEER, false);
return $h;
}
function do_request($handle, $url) {
curl_setopt($handle, CURLOPT_URL, $url);
$result = curl_exec($handle);
return json_decode($result, true);
}
Than you call:
$curl = init_my_curl();
do_request($curl, '//abc.com');
do_request($curl, '//abc.com/abc');
do_request($curl, '//abc.com/123');
curl_close($curl);
You can also wrap everything in class, but it depends on PHP version you are using and your code style.

Related

using php curl to pass variables to another site

I saw the following code on another post...
<?php
$ch = curl_init(); // create curl handle
$url = "http://www.google.com";
/**
* For https, there are more options that you must define, these you can get from php.net
*/
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_POST, true);
curl_setopt($ch,CURLOPT_POSTFIELDS, http_build_query(['array_of_your_post_data']));
curl_setopt($ch,CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT ,3); //timeout in seconds
curl_setopt($ch,CURLOPT_TIMEOUT, 20); // same for here. Timeout in seconds.
$response = curl_exec($ch);
curl_close ($ch); //close curl handle
echo $response;
?>
The only thing is I have no clue how to actually implement it or how it will work.
Here is what I am trying to do...
sitea.com/setup is setting php variables. If you visited that page, it would set $var1 = "hello" $var2="hi"
If someone visits siteb.com, I want to use php and somehow get those variables from sitea.com/setup to siteb.com and set them as new php variables. I'm assuming curl is the best option from what I've read, but can't figure out how to get it to work (or where to put it and how to call it) for that matter.
I'm experimenting with code trying to see how to do something for an upcoming project. Any help would be greatly appreciated.
I should note that I need this to be able to work from one domain on server1 to another domain on server2.
In a simple way it can be done like:
Site a: file.php
<?php
$a = 10;
$b = 20;
echo $a . ':' . $b;
?>
Site b: curl.php
<?php
$ch = curl_init(); // create curl handle
$url = "http://sitea/file.php";
/**
* For https, there are more options that you must define, these you can get from php.net
*/
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_POST, true);
curl_setopt($ch,CURLOPT_POSTFIELDS, http_build_query(['array_of_your_post_data']));
curl_setopt($ch,CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT ,3); //timeout in seconds
curl_setopt($ch,CURLOPT_TIMEOUT, 20); // same for here. Timeout in seconds.
$response = curl_exec($ch);
curl_close ($ch); //close curl handle
echo $response;
$parts = explode(':', $response);
$var1 = $parts[0];
$var2 = $parts[1];
?>

PHP: Multi Curl using Methods from some Library instead of URLs?

This is probably a stupid question, but I'm just wondering if this is possible or if I'm supposed to do something else...
When using multi-curl one would use URLs right?
// create both cURL resources
$ch1 = curl_init();
$ch2 = curl_init();
// set URL and other appropriate options
curl_setopt($ch1, CURLOPT_URL, "http://lxr.php.net/");
curl_setopt($ch1, CURLOPT_HEADER, 0);
curl_setopt($ch2, CURLOPT_URL, "http://www.php.net/");
curl_setopt($ch2, CURLOPT_HEADER, 0);
etc..
Per the multi-curl document...
So what if I have some method (I think thats what you call it) that I'm using from a library
$tags = $instagram->searchTags( 'tag' );
Now thats searching the library of the word tag. But what if I want to be able to do multiple searches,
$tags1 = $instagram->searchTags( 'tag' );
$tags2 = $instagram->searchTags( 'tagme' );
How do I implement this into multi curl? Is it just simply replacing the URLs with $tags1 and tags2?
This is without your class, dont see why you need that.
function fetchHTML($website) {
if(function_exists('curl_init')) {
$ch = curl_init($website);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
$content = curl_exec($ch);
curl_close($ch);
} else {
$content = file_get_contents($website)
}
return $content;
}
$dom = new DOMDocument();
$dom->loadHTML(fetchHTML("http://example1.com"));
$tag1 = $dom->getElementsByTagName('tagname');
$dom->loadHTML(fetchHTML("http://example2.com"));
$tag2 = $dom->getElementsByTagName('tagname');
/* Will give you a DOM object list with your first tagname */
print_r($tag1);
/* Will give you a DOM object list with your second tagname */
print_r($tag2);
I have looked into that PHP library anh found that each instance of the Instagram class use only one cURL handler, which leads to the fact that you cannot send multiple request asynchronously.
You can read this article about connection Sharing with CURL in PHP to get the idea of modify the CurlClient class of the Instagram library. The main idea here is keep a static class member which hold a handler from curl_multi_init() and add each new cURL single handler to it when you need.

How to add a string at the end of url

I use cURL to send requests to REST API and I would like to add a string to created url befeore execute it. This string is not a parameter.
Do you know how can I do that?
It's called string concatenation. In php it can be done: $str = $string1.$string2;
p.s. Always read documentation before asking something. There you can find answers on most of your questions.
It depends on really what you want to do. If you want to call an URL by appending GET query parameters, you can use string concatenation.
<?php
function get_data($url, $add) {
$url = $url.$add;
$ch = curl_init();
$timeout = 5;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
echo get_data('http://example.com', '?page=mine');
?>
However, if you want to retrieve a specific URL by sending POST query parameters (e.g. to simulate a form being submitted), you need to implement something like this instead.

Getting multiple http response codes using cURL?

Please take a look at this sample code:
function http_response($url)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, TRUE);
curl_setopt($ch, CURLOPT_NOBODY, TRUE); // remove body
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$head = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
echo $httpCode ;
}
this code will print the httpCode of the given url. I have couple of questions:
Can I get rid of some setopt() lines here and still getting httpCode?
What about if I want to check multiple urls at the same time? Can I modify the code to do that?
Can I do the same functionality in a simpler way using libraries different than cURL?
Thanks :)
You should be able to remove CURLOPT_HEADER and CURLOPT_NOBODY and still get the same result.
You could do that like this:
$urls = array(
'http://google.com',
'http://facebook.com'
);
$status = array();
foreach($urls as $url){
$status[$url] = http_response($url);
}
Try print_r($status); after this and you'll see the result.
You could do this with file_get_contents and $http_response_header, to learn more: http://www.php.net/manual/en/reserved.variables.httpresponseheader.php I would however recommend using cURL anyway.
*2. to check multiple urls you have to use this function in a loop, in any programming language 1 response from a server = 1 connection to that server. If you want to use 1 function to get responses from multiple servers you can always pass an array to the function and do the loop inside the function
*3. you can try this way:
function get_contents() {
file_get_contents("http://example.com");
var_dump($http_response_header);
}
get_contents();

To get around the ajax 'same origin policy', code for a PHP ajax request forwarder?

I want to bypass the ajax same-origin policy by having a php page on my site that basically acts like a JSON proxy. Eg i make an ajax request like this:
mysite.com/myproxy.php?url=blah.com/api.json&a=1&b=2
It then makes a request to:
blah.com/api.json?a=1&b=2
And returns the JSON (or whatever) result to the original requester.
Now i assume i'd be stupidly reinventing the wheel if i wrote this php code (plus i don't know php!) - is there some pre-existing code to do this? I'm sure i'm not the only one who's butted my head up against the same-origin policy before.
Oh yeah JSONP isn't an option for this particular api.
Thanks all
Okay, here's something -
Slap this into a php script, call it like this
script.php?url=blah
post the contents you want posted to the server.
<?php
$curlPost = http_build_query($_POST);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $_GET['url']);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost);
$data = curl_exec($ch);
curl_close($ch);
echo json_encode($data);
?>
Now this script is a bit too open for my liking, so to increase security I would recommend that you add a list of domains to a white list.
So add this to the top:
$whitelist = array('http://www.google.com','http://www.ajax.com');
$list = array();
foreach($whitelist as $w)
$list[] = parse_url($w,PHP_URL_HOST);
$url = $_GET['url'];
$url = pathinfo($url,PHP_URL_HOST);
if(!in_array($url, $list)) die('no access to that domain');

Categories