cURL with proxy based URL - php

I've to access content of this site http://www.autonet.ca/ using import.io api and it is based on Canada. It is accessible via browser proxy but I've to access it using cURL but it returns null.
$cc_se_page_url_autonet = "https://api.import.io/store/data/dd8eed56-4d41-4f43-aed6-ef8e7b3e9fc9/_query?input/webpage/url=http://www.autonet.ca/used-vehicles/montreal-qc/2010+2011+2012%20-honda-accord?price=0-5000&sortby=distance+asc&kmfrom=0&kmto=13000&_user=8df097bf-2f5d-4509-b13e-299d05bad826&_apikey=8df097bf-2f5d-4509-b13e-299d05bad826%3AH76otVMlVTG2KIW9fCZjWPtf4KzWFmlNBzbD2WIy9qOKSwIPgGxmUFVmTV9dDrORwcTtMBS1zZVLXSdEd9yfPQ%3D%3D";
// Initiate curl
$ch = curl_init();
// Disable SSL verification
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
// Will return the response, if false it print the response
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Set the url
#curl_setopt($ch, CURLOPT_URL, $cc_se_page_extractor_url_kijiji);
curl_setopt($ch, CURLOPT_URL, $cc_se_page_url_autonet);
// Execute
$result=curl_exec($ch);
// Closing
curl_close($ch);
// Will dump a beauty json :3
var_dump(json_decode($result, true));
If you copy/paste the $cc_se_page_url_autonet url in browser you will see the joson result but the cURL does not return the output? Any solution to access cURL along with proxy?
Output in browser is:
{
"offset":0,
"results":[
],
"cookies":[
"PHPSESSID=\"b5a335b54dd408f355726cd290c4d2ce\";Path=\"/\";Domain=\"www.autonet.ca\";Port=\"80\"",
"autonet_lastVisitedPage=\"%2Fused-vehicles%2Fmontreal-qc%2F2010%25202011%25202012%2520-honda-accord%3Fprice%3D0-5000\";Path=\"/\";Domain=\"www.autonet.ca\";Port=\"80\"",
"autonet_language=\"en\";Path=\"/\";Domain=\"www.autonet.ca\";Port=\"80\"",
"autonet_cityPostalCode=\"Montreal\";Path=\"/\";Domain=\"www.autonet.ca\";Port=\"80\"",
"autonet_userCity=\"Montreal%2C+QC\";Path=\"/\";Domain=\"www.autonet.ca\";Port=\"80\"",
"autonet_userCityPostalCode=\"Montreal\";Path=\"/\";Domain=\"www.autonet.ca\";Port=\"80\"",
"autonet_userCityRegion=\"6\";Path=\"/\";Domain=\"www.autonet.ca\";Port=\"80\"",
"BIGipServeredition.autonet.ca_pool80=\"3653435146.20480.0000\";Path=\"/\";Domain=\"www.autonet.ca\";Port=\"80\""
],
"connectorVersionGuid":"0603c6cf-eff5-46e2-b018-ad0bce333c5f",
"connectorGuid":"dd8eed56-4d41-4f43-aed6-ef8e7b3e9fc9",
"pageUrl":"http://www.autonet.ca/used-vehicles/montreal-qc/2010%202011%202012%20-honda-accord?price=0-5000",
"outputProperties":[
{
"name":"url",
"type":"STRING"
}
]
}

Related

How to get variable value from cURL

<?php
$message=$_POST["msg"];
echo $message;
// create a new cURL resource
$ch = curl_init();
// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, "http://.../pl1.php?
m=$message");
curl_setopt($ch, CURLOPT_HEADER, 0);
// grab URL and pass it to the browser
curl_exec($ch);
// close cURL resource, and free up system resources
curl_close($ch);
?>
Above is the code iam using.anyone suggest me how do i get value of m in pl1.php
I am getting following error:
Your browser sent a request that this server could not understand.
In PHP variables passed as query string inside URL are accessible in $_GET array.
In your case it will be $_GET['m'].
Try this code:
<?php
$message=$_POST["msg"];
echo $message;
// curl initialize
$ch = curl_init();
// set curl options
curl_setopt($ch, CURLOPT_URL, "http://.../pl1.php?
m=$message");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// execute request and return response
$output = curl_exec($ch);
echo $output;
// close cURL request
curl_close($ch);
?>
And to get that data on page pl1.php use $_GET['m'];
But, I believe sending data using curl with post method is better than get method.
Thank you

How to get json data in below format

JSON Data :
{"FORM_ID":51393,
"ORG_ID":1527,
"REQUEST_ACTION":"ADD",
"ADD_DATA":
{
"RECORDS":
[
{
"ID":"0",
"COLUMNS":
[
{"NAME":"App Status", "VALUE":"Pending"},
{"NAME":"Remarks","VALUE":"Application in Pending"},
{"NAME":"App Date (dd/MMM/yyyy)" ,"VALUE":"2017-29-03 00:00:00.0"},
{"NAME":"Applicant First Name", "VALUE":"TEST WEBSERVICES First"},
{"NAME":"Applicant Middle Name", "VALUE":"TEST WEBSERVICES Second"},
{"NAME":"Applicant Last Name", "VALUE":"TEST WEBSERVICES Last"},
{"NAME":"Enquiry Center - Walk", "VALUE":"Telephonic"},
{"NAME":"Admission City Office", "VALUE":"TestCity"},
{"NAME":"Applicant Email Id", "VALUE":"test#test.com"},
{"NAME":"Nationality", "VALUE":"testNationality"},
{"NAME":"Country", "VALUE":"INDIA"},
{"NAME":"State", "VALUE":"MAHARASTRA"},
{"NAME":"City", "VALUE":"Mumbai"},
{"NAME":"Permanent Country", "VALUE":"INDIA"},
{"NAME":"Application Seq No", "VALUE":""}
{"NAME":"Application Seq No", "Mobile No":"9999999999"}
]
}
]
}
}
I don't know what are you try to do ,but if u have array
echo json_encode($array);
if u try to get it from url
function curl_download($Url){
// is cURL installed yet?
if (!function_exists('curl_init')){
die('Sorry cURL is not installed!');
}
// OK cool - then let's create a new cURL resource handle
$ch = curl_init();
// Now set some options (most are optional)
// Set URL to download
curl_setopt($ch, CURLOPT_URL, $Url);
// Set a referer
curl_setopt($ch, CURLOPT_REFERER, "http://www.example.org/yay.htm");
// User agent
curl_setopt($ch, CURLOPT_USERAGENT, "MozillaXYZ/1.0");
// Include header in result? (0 = yes, 1 = no)
curl_setopt($ch, CURLOPT_HEADER, 0);
// Should cURL return or print out the data? (true = return, false = print)
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Timeout in seconds
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
// Download the given URL, and return output
$output = curl_exec($ch);
// Close the cURL resource, and free system resources
curl_close($ch);
return $output;
}
echo json_encode(curl_download("http://url.com")) ;

How to extract data from a decoded JSON object in php

I wanted to try to get data from a JSON string which is loaded from another page. I currently have used Curl to get the data from the webpage but I can't acces the data in it.
I've already tried:
var_dump(json_decode($result->version, true));
var_dump(json_decode($result[3][0]["date"], true));
But this does't seem to work as it always returns NULL
$url="https://roosters.deltion.nl/api/roster?group=AO2B&start=20160125&end=20160201";
// Initiate curl
$ch = curl_init();
// Disable SSL verification
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
// Will return the response, if false it print the response
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Set the url
curl_setopt($ch, CURLOPT_URL,$url);
// Execute
$result=curl_exec($ch);
// Closing
curl_close($ch);
// Will dump a beauty json :3
var_dump(json_decode($result, true));
First decode the JSON, then get the properties you want. Like this:
$yourObject = json_decode($result);
var_dump($youObject->version);
this is working for me.
<?php
$url = "https://roosters.deltion.nl/api/roster?group=AO2B&start=20160125&end=20160201";
// Initiate curl
$ch = curl_init();
// Disable SSL verification
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
// Will return the response, if false it print the response
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Set the url
curl_setopt($ch, CURLOPT_URL, $url);
// Execute
$result = curl_exec($ch);
// Closing
curl_close($ch);
// Will dump a beauty json :3
$data = json_decode($result);
//echo $data->data[0]['date'];
echo "<pre>";
print_r($data->data[0]->date);
}
?>
if you want to get date of all index then try this in loop.
Firstly if your using GET there is no need to use CURL,
$result = file_get_contents(https://roosters.deltion.nl/api/roster?group=AO2B&start=20160125&end=20160201);
Will work just as well without any of the overhead. I suspect that your CURL isn't returning the page content so using file_get_contents() will fix it.

sending xml string using php curl but not as post parameter

:)
I'm trying to send an XML using curl but not as post parameter. what I mean is this.
for example.
the receiving side of that XML won't be able to recieve the XML using $_POST variable.
he will need to use the following code:
$xmlStr=null;
$file=fopen('php://input','r');
$xmlStr=fgets($file);
I want to be able to send an xml string using curl via https.
so the following would be wrong:
public static function HttpsNoVerify($url,$postFields=null,$verbose=false) {
// Initialize session and set URL.
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
// Set so curl_exec returns the result instead of outputting it.
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
if ($postFields !=null) {
curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields);
}
if ($verbose) {
curl_setopt($ch, CURLOPT_VERBOSE, 1);
}
// Get the response and close the channel.
$response = curl_exec($ch);
curl_close($ch);
return $response;
}
because here i can use HttpsNoVerify($url,array('xml_file'=>'xml..')); and that
will paste it as post parameter. and i want it as post output.
so please I hope i explained myself properly and I explained exactly what I don't want to do.
how can I do what i want to do?
thanks! :)
kfir
Just directly pass the xml string as second parameter instead of an associative array item,
HttpsNoVerify($url, 'xml ..');
This will eventually call
curl_setopt($ch, CURLOPT_POSTFIELDS, "xml ...");
Which will be put in php://input for the remote server.

cURL operation and PHP

I have a frontend code
$ch = curl_init();
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
//curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept: application/json'));
curl_setopt($ch, CURLOPT_URL, $url);
//make the request
$responseJSON = curl_exec($ch);
$response_status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($response_status == 200) { // success
// remove any "problematic" characters from the json string and then decode
if (debug) {
echo "----finish API of getAPI inside basic_function with status==200---";
echo "<br>";
echo "-------the json response is-------" ; //.$responseJSON;
var_dump($responseJSON);
//print_r($responseJSON);
echo "<br>";
}
return json_decode( preg_replace( '/[\x00-\x1F\x80-\xFF]/', '', $responseJSON ) );
}
and I have a backend code which executed when cURL fired its operation with its URL. The backend code would therefore activated. So, I know cURL is operating.
$output=array (
'status'=>'OK',
'data'=>'12345'
)
$output=json_encode($output)
echo $output;
and $output shown on browser as {"status":"OK","data":"12345"}
However, I gone back to the frontend code and did echo $responseJSON, I got nothing. I thought the output of {"status":"OK","data":"12345"} would gone to the $responseJSON. any idea?
Here's output on Browser, something is very odd! the response_status got 200 which is success even before the parsing of API by the backend code. I expect status =200 and json response after the {"status":"OK","data":"12345"}
=========================================================================================
inside the get API of the basic functions
-------url of cURL is -----http://localhost/test/api/session/login/?device_duid=website&UserName=joe&Password=1234&Submit=Submit
----finish API of getAPI inside basic_function with status==200---
-------the json response is-------string(1153)
"************inside Backend API.php******************
---command of api is--/session/login/
---first element of api is--UserName=joe
--second element of api is---Password=1234
---third element of api is----Submit=Submit
----fourth element of api is---
-------inside session login of api-------------
{"status":"OK","data":"12345"}
Have you tried with curl_setopt($ch, CURLOPT_TIMEOUT, 10); commented?
See what happends if you comment that line.
Also try with the a basic code, if that works, smthing you added later is wrong:
// create a new cURL resource
$ch = curl_init();
// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, "http://www.example.com/");
curl_setopt($ch, CURLOPT_HEADER, false);
// grab URL and pass it to the browser
curl_exec($ch);
// close cURL resource, and free up system resources
curl_close($ch);
Try var_dump($responseJSON)
If it returns false try
curl_error ( $ch )
Returns a clear text error message for the last cURL operation.
Are you sure your $url is correct?

Categories