Adding prefix to url for use with PHP CURL function - php

I'm trying to get some data from a website using PHP Curl as follows:-
$url_1 = "website.com"
$url_2 = "http://www.website.com"
$url_3 = "http://www." . $url_1;
$ch = curl_init($url_1); // failure
$ch = curl_init($url_2); // success
$ch = curl_init($url_3); // failure
I have a huge list of URLS in the format of $url_1 please will you let me know how I can add the http:// prefix to the url so it can be accepted by curl_init()
Thanks

Try $ch = curl_init("http://www." . trim($url_1));

Related

how to pass dynamic URL to curl in php - getting error 1

I'm trying to pass a $url to curl using a function.
the URL is built with a variable in it in the following method:
a.php // main page, include (a.php, b.php)
b.php // dynamic string function
c.php // curl function
I build a dynamic string successfully using sessions data // $_SESSION["input"]
myDynamicstringfunction set a string by multiple sessions input values.
$dval = myDynamicstringfunction();
echo $dval;
// render correctly to: "-e5 -g6 -g7"
the $dval value is a string that resolve as expected. the $url is:
$url = "https://someurl.com/a/b?dc=-cv1.5 -a1 -b2 -c3 -d4 $dval";
The $url is render correctly with the
echo $url;
$url = "https://someurl.com/a/b?dc=-cv1.5 -a1 -b2 -c3 -d4 -e5 -g6 -g7";
I pass the $url to the curl function using:
$r = mycUrlfunction($url);
The curl function I use:
function singleRequest($url){
$ch = curl_init();
// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPGET, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
// grab URL and pass it to the browser
$curly = curl_exec($ch);
if ($curly == FALSE){
die("cURL Error: " . curl_error($ch));
}
$result = json_decode($curly, true);
// close cURL resource, and free up system resources
curl_close($ch);
echo '<pre>';
return ($result);
}
The above get me an error (curl 1) - CURLE_UNSUPPORTED_PROTOCOL (1)
I have tried many things to get the $url to work with no success.
if I set the $url value manually without the $dval variable like this:
$url = "https://someurl.com/a/b?dc=-cv1.5 -a1 -b2 -c3 -d4 -e5 -g6 -g7";
The code works just fine and I get the correct results from the API call.
I tried using different quotes, {}, [], encoding to ASCII, vprintf(), and other solutions with no success.
the problem was with constructing the dynamic variable $dynamicstring of the URL
initially used
$dynamicstring= "-a" . $_SESSION["a"]." -b".$_SESSION['b']." -c".$_SESSION['c']." -d".$_SESSION['d']."<br>";
this have a few problems
when using echo it render the expected output correctly
it have a "<br>" at the end
it have the wrong structure
the correct way to construct the $dynamicstring is to use {}
$dynamicstring= "-a{$_SESSION["a"]} -b{$_SESSION['b']} -c{$_SESSION['c']} -d{$_SESSION['d']}";

How to pass GET parameters in curl PHP

I am new to PHP development. I am working on CURL to call my WEB API. As a newbie I found very hard to understand things.
How my API is working
API_URL is http://localhost:14909/api/meters/GetByMsn/002999000077/2017-10-11T12:16:20
It takes a meter serial number and a data time and gives the response by authorizing the URL. The response I get is
{
"data": {
"Response": "No"
}
}
What I want to do
Now In PHP I am using CURL to make the request. The request is simple as it takes the current selected meter serial number and a current date time also it should take authorization key.
What I have done till now
Below is the code so far i have done
if( isset($_REQUEST['selected_meters']))
{
$m = MetersInventoryStore::findOne($_REQUEST['selected_meters']);
$msn = $m->meter_serial; // current selected meter serial number is saved
$date_time = str_replace(' ','T',date('Y-m-d H:i:s')); // current date time
$api_url = 'http://116.xx.xx.xx:xxxx/api/meters/GetByMsn/'; // my base URL
$curl = curl_init($api_url);
curl_setopt($curl,CURLOPT_RETURNTRANSFER, CURLOPT_HTTPHEADER, array('Authorization: MY_KEY')); // setting the authorization key in header.
exit();
}
Now I want to send the meter serial number and date time parameters. For this I have searched many articles but all I found a way to pass parameters as query and related link.
One method I am thinking of is passing parameters direct to the URL Like:
$api_url = 'http://116.xx.xx.xx:xxxx/api/meters/GetByMsn/[$msn]/[$date_time]';
OR
$api_url = 'http://116.xx.xx.xx:xxxx/api/meters/GetByMsn/' + $msn + '/'+$date_time;
But I don't know will it works or not
Any help would be highly appreciated.
Try this out and see if it works:
$api_url = 'http://116.xx.xx.xx:xxxx/api/meters/GetByMsn/${msn}/${date_time}';
or
$api_url = 'http://116.xx.xx.xx:xxxx/api/meters/GetByMsn/{$msn}/{$date_time}';
So after a lot of searching I manage to get a response. Concatenate both of the parameters in the URL and changing the curl_setopt.
Changes:
From
$api_url = 'http://116.xx.xx.xx:xxxx/api/meters/GetByMsn/'; // my base URL
To
$api_url = 'http://116.xx.xx.xx:xxxx/api/meters/GetByMsn/'.$msn . '/' . $date_time; // my base URL
And
curl_setopt($curl,CURLOPT_RETURNTRANSFER, CURLOPT_HTTPHEADER, array('Authorization: MY_KEY')); // setting the authorization key in header.
To
curl_setopt($curl CURLOPT_HTTPHEADER, array('Authorization: MY_KEY')); // Removed the CURLOPT_RETURNTRANSFER
And then
$curl_response = curl_exec($curl);
print_r($curl_response);
/* print_r($msn);
echo $date_time;*/
//echo date('Y-m-d H:i:s');
exit();

Unable to send URL

I am using CURL to send a URL. It's not sending an URL which contains PHP variables although is working perfectly on a defined URL (not incuding any PHP variable).
For example: This link is not working because of PHP variables :
$url = "http://abc/create/name/{$firstname} {$lastname}/email/{$email}/password/{$password1}?level={$level}&session=Dec";
$request = curl_init($url);
$response = curl_exec($request);
var_dump($response);
This works fine: Contains static values
$url = "http://abc/create/name/any one/email/anyone#gmail.com/password/12345?level=1&session=Dec";
$request = curl_init($url);
$response = curl_exec($request);
var_dump($response);
What am I doing wrong? Any leads?
Note: The URL is perfectly echoed, No error in echo $url
please putt like this and it will work
$url = 'http://abc/create/name/{'.$firstname.'}{'.$lastname.'}/email/{'.$email.'}/password/{'.$password1.'}?level={'.$level.'}&session=Dec"';
Finally got the solution :)
I am giving a space between in my {$firstname} {$lastname}thats why its not sending the URL.
So i just concatenate my firstname and lastname in a single variable and it works like a charm.
$fullname = $get_user_firstname.$get_user_lastname;
$fullname = urlencode($fullname);
$url = "http://abc/create/name/{$fullname}/email/{$email}/password/{$password1}?level={$level}&session=Dec";
Hope anyone with similar issue can get help from my this answer that's why i posted it. cheers!

PHP - Getting a URL within a function argument

I have the below function that works perfect when I put the URL string within the argument manually. I need it to be dynamic though and I am using Wordpress.
function get_tweets($url) {
$json_string = file_get_contents('http://urls.api.twitter.com/1/urls/count.json?url=' . $url);
$json = json_decode($json_string, true);
return intval( $json['count'] );
}
// Below is the one that works manually
<?php echo get_tweets('http://www.someurl.com');
//ones I have tried that do not (trying to make dynamic)
$url = $get_permalink();
echo get_tweets('$url');
echo get_tweets($url);
$url = '$get_permalink()';
$url = $get_permalink(); // produces needs to be in string error
echo get_tweets($url);
There is nothing wrong with what you're doing, per se. The only obvious mistake I can see is that you aren't encoding the URL properly. You need to ensure the query string arguments you put in the URL are properly URL encoded, otherwise the remote host may not interpret the request correctly.
function get_tweets($url) {
$json_string = file_get_contents('http://urls.api.twitter.com/1/urls/count.json?url=' . urlencode($url));
$json = json_decode($json_string, true);
return intval( $json['count'] );
}
echo get_tweets('http://www.someurl.com'); // should work just fine
Did you try to urlencode your url String?
urlencode($foo);
Your main problem is on below line
Change
//ones I have tried that do not (trying to make dynamic)
$url = $get_permalink();
To
//ones I have tried that do not (trying to make dynamic)
$url = get_permalink();

how to find the total no.of inbound and outbound links of a website using php?

how to find the total no.of inbound and outbound links of a website using php?
To count outbound links
parse html for webpage
parse all links using regex
filter links which starts with your domain or "/"
To inbound link
Grab google results page
http://www.google.ca/search?sourceid=chrome&ie=UTF-8&q=site:
parse similarly
For outbound links, you will have to parse the HTML code of the website as some here have suggested.
For inbound links, I suggest using the Google Custom Search API, sending a direct request to google can get your ip banned. You can view the search api here. Here is a function I use in my code for this api:
function doGoogleSearch($searchTerm)
{
$referer = 'http://your-site.com';
$args['q'] = $searchTerm;
$endpoint = 'web';
$url = "http://ajax.googleapis.com/ajax/services/search/".$endpoint;
$args['v'] = '1.0';
$key= 'your-api-key';
$url .= '?'.http_build_query($args, '', '&');
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_REFERER, $referer);
$body = curl_exec($ch);
curl_close($ch);
//decode and return the response
return json_decode($body);
}
After calling this function as: $result = doGoogleSearch('link:site.com'), the variable $result->cursor->estimatedResultCount will have the number of results returned.
PHP can't determine the inbound links of a page through some trivial action. You either have to monitor all incoming visitors and check what their referrer is, or parse the entire internet for links that point to that site. The first method will miss links not getting used, and the second method is best left to Google.
On the other hand, the outbound links from a site is doable. You can read in a page and analyze the text for links with a regular expression, counting up the total.
function getGoogleLinks($host)
{
$request = "http://www.google.com/search?q=" . urlencode("link:" . $host) ."&hl=en";
$data = getPageData($request);
preg_match('/<div id=resultStats>(About )?([\d,]+) result/si', $data, $l);
$value = ($l[2]) ? $l[2] : "n/a";
$string = "" . $value . "";
return $string;
}
//$host means the domain name

Categories