unable to send data using CURLOPT_POST to other php file - php

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
)

Related

PHP rest service

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>";
?>

JSON array foreach loop showing Invalid argument supplied for foreach()

I want to display http://services.xyz.com/tours.asmx/Sample?type=2&CityID=1146&days=4 this array data in HTML.
Here is my code:
ini_set("display_errors", 1);
$url = "http://services.xyz.com/tours.asmx/Sample?type=2&CityID=1146&days=4";
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
//curl_setopt($ch, CURLOPT_POSTFIELDS, $fields); // set the fields to post
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // make sure we get the response back
$xml = curl_exec($ch); // execute the post
curl_close($ch); // close our session
include "XMLParser.php";
$parser = new XMLParser();
$tour = $parser->parseString($xml);
print_r($tour);
//$errors = array_filter($tour);
if (empty($tour)) {
echo 'Error';
}else{
echo 'No Error';
}
//print_r($xml);
//var_dump( $xml);
//echo $xml;
//$xmlfile = file_get_contents($tour);
$ob= simplexml_load_string($tour);
//$json = json_encode($ob);
$configData = json_decode($ob, true);
foreach($configData as $result){
echo '<tr>';
echo '<td>'.$result->name.'</td>';
echo '<td>'.$result->phone.'</td>';
echo '<td>'.$result->email.'</td>';
echo '</tr>';
}
I am getting this error Warning: Invalid argument supplied for foreach() in C:
Please help me.
Thanks
Here is my final Working code to help someone else who need this code.
<?php
ini_set("display_errors", 1);
//$days = "http://services.xyz.com/tours.asmx/Days?type=2&cityID=1146";
$url = "http://services.xyz.com/tours.asmx/Sample?type=2&CityID=1146&days=4";//Replace this with your own Web services Link
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // make sure we get the response back
$xml = curl_exec($ch); // execute the post
curl_close($ch); // close our session
$var = simplexml_load_string($xml);
$configData = json_decode($var, true);
/*echo '<pre>';
print_r($configData);
echo '</pre>';*/
foreach ($configData as $row) {
echo 'Day:' . $row['Day'].'<br>';
echo 'Time:' .$row['Time'].'<br>';
echo 'Description:' .$row['Description'].'<br>';
}?>

file_get_contents phase XML

Trying to run file_get_contents from http://66.85.14.212:7254
Anyone have any suggestions how I could do this to query specific values?
I am getting the value by given code(it's working properly.):-
<?php
$url="http://66.85.14.212:7254/";
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
$data = curl_exec($ch);
curl_close($ch);
$xml = simplexml_load_string($data);
echo "<pre>";
print_r($xml);
//for single variable you can write
echo $xml->Name;
?>

$_POST via cURL not working

Im trying my hand at using curl to post some data, I am not reciving my post content and can not find the source of the problem
curl.php
<?php
$data = array("user_email" => "22" , "pass" => "22" );
$string = http_build_query($data);
$ch = curl_init("http://localhost:8888/290_project/test.php"); //this is where post data goes too, also starts curl
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_exec($ch);
curl_close($ch) //ends curl
?>
test.php
<?php
if(isset($_POST['user_email'], $_POST['pass'])) {
$name = $_POST['user_email'];
$pass = $_POST['pass'];
echo $name;
echo $pass;
} else {
echo "error";
} ?>
Every time I get my error response meaning the post data is not going through. I have tried everything I could think of to trouble shoot;I must be over looking something I am simply not yet familiar with?
Please set CURLOPT_URL to http://localhost:8888.....
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://localhost:8888/290_project/test.php");
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_exec($ch);
if(!curl_errno($ch)){
$info = curl_getinfo($ch);
} else {
echo 'Curl error: ' . curl_error($ch);
}
curl_close($ch) //ends curl
?>
With curl_getinfo() - Gets information about the last transfer.
For more detail read below link:- http://php.net/manual/en/function.curl-getinfo.php
I have edited the answer, The reason for error is not curl.
http_build_query($data, '', '&');
Following is working example. Please try this.
$postData = array(
'user_name' => 'abcd',
'password' => 'asdfghj',
'redirect' => 'yes',
'user_login' => '1'
);
$url='http://localhost:8888/290_project/test.php';
$ch = curl_init();
//Set the URL to work with
curl_setopt($ch, CURLOPT_URL, $url);
// ENABLE HTTP POST
curl_setopt($ch, CURLOPT_POST, 1);
//Set the post parameters
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
//execute the request
$store = curl_exec($ch);
print_r($store);
curl_close($ch)
You have an error in your php code:
edit:
if(isset($_POST['user_email'], $_POST['pass'])) {
to:
if(isset($_POST['user_email']) && isset($_POST['pass'])) {

How to echo the cURL result in PHP?

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!

Categories