I'm new to PHP and i'm trying to send this of to a webservice.
<?php
$myID = "8125987";
$myVarible = "This is a test";
$service_url = "https://...?password=123&user=123&storeid=1000&mobile=$myID&message=$myVarible";
$curl = curl_init($service_url);
$result = curl_exec($curl);
curl_close($curl);
echo $result;
?>
It is not working, what is wrong?
Thanks for any help.
KHJ
1) Check if your curl function exists first
echo function_exists('curl_version') ? 1:0; // 1 = enabled , 0 =
disabled.
2) Try to access example.com instead of your target url
$url = "http://www.example.com";
3) Print out the curl info and err message before you asked a question
There're lots of CURL examples on the internet, however, I still did one for your reference.
https://www.stockeasymoney.com/ns/sit/curlRaw.php
<?php
echo function_exists('curl_version') ? 1:0; // 1 = enabled , 0 = disabled.
echo "<pre>";
$url = "http://www.example.com";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_USERAGENT, "MozillaXYZ/1.0");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
$result = curl_exec($ch);
print_r( curl_getinfo($ch) );
curl_close($ch);
var_dump($result);
echo "</pre>";
?>
Related
I have some problem that related to HTTP_HEADERS in curl php in opencart. The code below is caller.
$ch = curl_init();
$url = 'http://aaa.com/index.php?route=common/home/getTotalCustomer';
$url2 = 'http://bbb.com/index.php?route=common/home/getTotalCustomer';
$url3 = 'http://ccc.com/index.php?route=common/home/getTotalCustomer';
$header = array('Authorization:Basic ' . base64_encode('user:password'));
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FAILONERROR, true);
curl_setopt($ch, CURLOPT_HEADER, false);
$results = curl_exec($ch);
echo '<pre>';
print_r(json_decode($results, true));
echo '</pre>';
The receiver code like below:
public function getTotalCustomer(){
$json = array();
$this->load->model('account/customer');
if(isset($_SERVER['PHP_AUTH_USER']) && isset($_SERVER['PHP_AUTH_PW'])){
if(($_SERVER['PHP_AUTH_PW'] == 'password') && ($_SERVER['PHP_AUTH_USER'] == 'user')){
$json['total_customer'] = $this->model_account_customer->getTotalCustomer();
}
} else{
$json['message'] = 'failed';
}
$this->response->addHeader('Content-Type: application/json');
$this->response->setOutput(json_encode($json));
}
I had tried in multiple domain with different servers. Some server can return the data but some server cannot return the data. Why?
Your header that you're sending is incorrect.
You're passing
Authorization:Basic <username:password>
it should be
Authorization: Basic <username:password>
note the space.
I am new to php , i am trying to get data from a website using curl (scraping),
UNABLE to get data from index.php to data.php, using CURLOPT_POST.. what am i doing wrong..?
followed this tutorial on youtube
index.php
<?php
$data = array("name"=>"john","age"=>31);
$string = http_build_query($data);
echo $string;
$ch = curl_init("http://localhost/scrap_practise/data.php");
curl_setopt($ch, CURLOPT_POST,true);
curl_setopt($ch, CURLOPT_POSTFIELDS,$string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_exec($ch);
curl_close($ch);
?>
data.php
<?php
echo 'finlly in'; // this never echos
if(isset($_POST['name'],$_POST['age'])){
$db = new Mysqli("localhost","root","","mydb");
$name = $db->real_escape_string($_POST['name']);
$age = (int) $_POST['age'];
$query = "INSERT INTO data SET data='$name,$age'";
$db->query($query);
}
?>
Simply you have to update you index.php script with these line of code.
index.php
<?php
$data = array("name"=>"john","age"=>31);
$string = http_build_query($data);
echo $string;
$ch = curl_init("http://localhost/scrap_practise/data.php");
curl_setopt($ch, CURLOPT_POST,true);
curl_setopt($ch, CURLOPT_POSTFIELDS,$string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
if( ! $result = curl_exec($ch))
{
trigger_error(curl_error($ch));
}
curl_close($ch);
// to see the return result uncomment the below line code.
//print_r($result);
?>
For more options or function reference see this link - http://php.net/manual/en/function.curl-exec.php
Hope this will help to resolve your issue !!
Try this code, I hope it will work for you...
<?php
$url = 'http://localhost/scrap_practise/data.php';
$data = array("name"=>"john","age"=>31);
$string = http_build_query($data);
echo $string;
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST,true);
curl_setopt($ch, CURLOPT_POSTFIELDS,$string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
print_r($result) ;
curl_close($ch);
?>
In data.php file
<?php
echo 'finlly in'; // this never echos
if(isset($_POST['name'],$_POST['age']))
{
echo "<pre>"; print_r($_POST);
}
?>
This will Output :
finlly in
Array
(
[name] => john
[age] => 31
)
Here is a URL→ https://ajax.googleapis.com/ajax/services/search/images?v=1.0&q=snoopy&rsz=1
and this is my php code
<?php
$url = "https://ajax.googleapis.com/ajax/services/search/images?"."v=1.0&q=snoopy";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$body = curl_exec($ch);
curl_close($ch);
$json = json_decode($body);
?>
I wanna echo all of the image urls from its result
(For example: http://img2.wikia.nocookie.net/__cb20110331075248/peanuts/images/6/62/Snoopy.gif )
But I have no idea how to echo them.
Hope someone could help me, thanks!
Maybe like this:
<?php
$url = "https://ajax.googleapis.com/ajax/services/search/images?" .
"v=1.0&q=barack%20obama&userip=INSERT-USER-IP";
// sendRequest
// note how referer is set manually
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_REFERER, 'http://example.com');
$body = curl_exec($ch);
curl_close($ch);
// now, process the JSON string
$pics = json_decode($body);
$ret='';
if($pics){
foreach($pics->responseData->results as $pic)
$ret .= $pic->url.'<br>';
}
echo $ret;
?>
in order to echo the exact result just use:
echo $body; //echoes json string
if you want to echo the exact link, echo like this:
echo $json->responseData->results[0]->url; //echoes http://img2.wikia.nocookie.net/__cb20110331075248/peanuts/images/6/62/Snoopy.gif
or you can foreach() twice (or once...) then echo $res->url directly :) your choise!
I've been writing some php script ..
$gettarget = $argv[2];
echo "Trying to do something . . .\n";
sleep(2);
$ch = curl_init($gettarget); // initialize curl with given url
$useragent = "Some user agent ";
curl_setopt($ch, CURLOPT_USERAGENT, $useragent); // add useragent
$response = curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // write the response to a variable
echo $response;
echo "Done";
So here's what happens , It comes to " Trying to do something " and then nothing happens and the Done doesn't get echoed that means there's something wrong with curl , I don't really know what is up with cURL , So I thought I would post here.
You have not executed curl yet, in this code block. Need a $response = curl_exec( $ch ); at the end.
$gettarget = $argv[2];
echo "Trying to do something . . .\n";
sleep(2);
$ch = curl_init($gettarget); // initialize curl with given url
$useragent = "Some user agent ";
curl_setopt($ch, CURLOPT_USERAGENT, $useragent); // add useragent
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // setopt() does not exec()
// write the response to a variable
$response = curl_exec( $ch );
echo $response;
echo "Done";
$response = curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
should be
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
The curl_setopt() call tells it to return the response when you call curl_exec()
if i understand the google error OVER_QUERY_LIMIT right, than it comes because of too many ip-requests.
BUT if I go to the browser and load the following link:
http://maps.googleapis.com/maps/api/geocode/xml?address=Paris&sensor=true
I can see the xml!
So why is that?
Do I have a problem in my code?
Here it is:
$the_map = htmlspecialchars($_POST['map'], ENT_QUOTES);
error_reporting(0);
$map_query = file_get_contents('http://maps.google.com/maps/api/geocode/xml?address='.rawurlencode($the_map).'&sensor=false');
error_reporting(1);
if ($map_query){
$geo_code = simplexml_load_string(utf8_encode($map_query));
if ($geo_code){
$map_lat = $geo_code->xpath("/GeocodeResponse/result/geometry/location/lat");
$map_lng = $geo_code->xpath("/GeocodeResponse/result/geometry/location/lng");
$map_lat = $map_lat[0][0];
$map_lng = $map_lng[0][0];
}
}
Use curl instead of file_get_contents:
$address = "India+Panchkula";
$url = "http://maps.google.com/maps/api/geocode/json?address=$address&sensor=false®ion=India";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_PROXYPORT, 3128);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
$response = curl_exec($ch);
curl_close($ch);
$response_a = json_decode($response);
echo $lat = $response_a->results[0]->geometry->location->lat;
echo "<br />";
echo $long = $response_a->results[0]->geometry->location->lng;
Or See the below URL
Warning while using file_get_contents for google api