Retrieving URL formed by CURL POST - php

I want to retrieve the final URL formed using a CURL request, but none of them work what I want to retrieve.
for example - I directly paste an API call in the browser address bar, it works, but when using CURL request I get some errors.
To find that I want to retrieve the final request URL formed by CURL or all the request parameters included in CURL Request.
The various methods I have tried is like:
$ch = curl_init();
$f = fopen('request.txt', 'w');
curl_setopt_array($ch, array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_FOLLOWLOCATION => 1,
CURLOPT_VERBOSE => 1,
CURLOPT_STDERR => $f,
));
$response = curl_exec($ch);
fclose($f);
curl_close($ch);
Alternatively -
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
$data = curl_exec($ch);
var_dump($data);
var_dump(curl_getinfo($ch));
Thanks.
Update:
My request should form url like this:
https://api-3t.sandbox.paypal.com/nvp/?METHOD=SetExpressCheckout&VERSION=86&PWD=9mypassword&USER=my_user_name&SIGNATURE=my_api_signature&L_BILLINGTYPE0=RecurringPayments&L_BILLINGAGREEMENTDESCRIPTION0=FitnessMembership&cancelUrl=http://localhost/recurring-payment/index.php&returnUrl=http://localhost/recurring-payment/review.php
Now I want to get all the info in the above URL being submitted to Paypal API.
So I can retrieve this info being sent through CURL.

Related

CartoDB - SQL API - Post request with php

In my app I'd like to send some small amount of data to my cartodb-table with my php-script.
I would like to use the SQL-API. My app parse the data in the form like:
https://{account}.cartodb.com/api/v2/sql?q=INSERT INTO test_table (column_name, column_name_2, the_geom) VALUES ('this is a string', 11, ST_SetSRID(ST_Point(-110, 43),4326))&api_key={Your API key}
I tried several functions http_get, file_get_contents, http_request but nothing pass the data to my account. Put when I copy/paste the URL and open in the browser, everything is added.
What's the right function? There so many different ones... PHP-HTTP-Functions
EDIT
With the code from this solution I get this error, when I print out the response:
{"error":["syntax error at end of input"]}
within the browser I get this:
{"rows":[],"time":0.038,"fields":{},"total_rows":1}
my request code right now:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $fullurl);
// Set so curl_exec returns the result instead of outputting it.
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Get the response and close the channel.
$response = curl_exec($ch);
curl_close($ch);
print($response);
Found the result: with CURLOPT_POST => 1 it works!
$ch = curl_init();
curl_setopt_array($ch, array(
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => "https://".$cartodb_key.".cartodb.com/api/v2/sql",
CURLOPT_USERAGENT => 'Sample cURL Request',
CURLOPT_POST => 1,
CURLOPT_POSTFIELDS => array(
api_key => $api_key,
q => "INSERT INTO spot (".implode (", ", array_keys($data)).") VALUES (".implode (", ", $data).")"
)
));
$response = curl_exec($ch);
curl_close($ch);

How to call an API url from php?

I'm trying to call an API (mapquest) url from a shared host.
The url works fine, as it shows the expected JSON response when pasted in a browser.
However, I can't make it work from a php page.
I tried both curl and file_get_contents with no success.
I keep on getting HTTP/1.1 400 Bad Request error...
Here is the code I use, which is quite basic...
$ch=curl_init();
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER, TRUE);
$result=curl_exec($ch);
if(!$result){
exit ('cURL ERROR: '.curl_error($ch));
}
return var_dump($result);
Hello try some thing like this
$query = urlencode('where={"steps":9243}');
$ch = curl_init('https://api.parse.com/1/classes/Steps?'.$query);
curl_setopt($ch,CURLOPT_HTTPHEADER,array('X-Parse-Application-Id: myApplicationID',
'X-Parse-REST-API-Key: myRestAPIKey',
'Content-Type: application/json'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_exec($ch);
curl_close($ch);
refer this link
Querying API through Curl/PHP
I got it!
The url was composed of JSON data with spaces.
This part of the URL must be encoded with urlencode(), but not the full url.
It works both with curl and file_get_contents().
Try below code
$ch = curl_init();
$curlConfig = array(
CURLOPT_URL => "http://www.bing.com/",
CURLOPT_POST => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POSTFIELDS => array(
'field1' => 'some date',
'field2' => 'some other data',
)
);
curl_setopt_array($ch, $curlConfig);
echo $result = curl_exec($ch);
curl_close($ch);

Getting images from Instagram, always null

I develop a small script on localhost (WAMP server, CURL enabled) to retrieve images from Instagram. Based on this script: see tutorial I have also set up the variables on Instagram. I'm not sure what is wrong more specifically or how to solve it (probably with authentication).
USER ID
CLIENT ID
CLIENT SECRET
WEBSITE URL
REDIRECT URI
This is my user
http://instagram.com/krondorl
function fetchData($url){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 20);
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
$result = fetchData("https://api.instagram.com/v1/users/280766222/media/recent/?access_token=df04c6989eaf4490bdac1f82554182bb");
$result = json_decode($result);
var_dump($result);
Try the code below which works for me. Make sure your URL is correct.
$json_url ='https://api.instagram.com/v1/users/280766222/media/recent/?access_token=df04c6989eaf4490bdac1f82554182bb';
// Initializing curl
$ch = curl_init( $json_url );
// Configuring curl options
$options = array(
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => array('Content-type: application/json')
);
// Setting curl options
curl_setopt_array( $ch, $options );
// Getting results
$results = curl_exec($ch); // Getting jSON result string
$json_decoded = json_decode($results);
By the way I check your URL which happens to be incorrect and give me the below error.
{"meta":{"error_type":"OAuthAccessTokenException","code":400,"error_message":"The \"access_token\" provided is invalid."}}
Before you trying the code, try the URL on the browser and see whether the URL is correct.

How to proxy another page in PHP

I'm looking for the fastest and easiest way to proxy a page in PHP. I don't want the user to be redirected, I just want my script to return the same content, response code and headers as another remote URL.
echo file_get_contents('proxypage');
Would that work?
EDIT:
First answer was a bit short, and I don't believe it will handle headers as you would like.
However you can also do this:
function get_proxy_site_page( $url )
{
$options = [
CURLOPT_RETURNTRANSFER => true, // return web page
CURLOPT_HEADER => true, // return headers
CURLOPT_FOLLOWLOCATION => true, // follow redirects
CURLOPT_ENCODING => "", // handle all encodings
CURLOPT_AUTOREFERER => true, // set referer on redirect
CURLOPT_CONNECTTIMEOUT => 120, // timeout on connect
CURLOPT_TIMEOUT => 120, // timeout on response
CURLOPT_MAXREDIRS => 10, // stop after 10 redirects
];
$ch = curl_init($url);
curl_setopt_array($ch, $options);
$remoteSite = curl_exec($ch);
$header = curl_getinfo($ch);
curl_close($ch);
$header['content'] = $remoteSite;
return $header;
}
This will return you an array containing lots of information on the remote page. $header['content'] will have both the content of the website and the headers, $header[header_size] will contain the length of that header so you can use substr to split those up.
Then it's just a matter of using echoand header to proxy the page.
You can use the PHP cURL functions to achieve this functionality:
http://www.php.net/curl
// 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, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// grab URL and pass it to the browser
$urlContent = curl_exec($ch);
From this point, you would grab the response header information using http://www.php.net/curl-getinfo. (There are several values you can grab, all listed in the documentation).
// Check if any error occured
if(!curl_errno($ch))
{
$info = curl_getinfo($ch);
header('Content-Type: '.$info['content_type']);
echo $urlContent;
}
Make sure to close out the cURL handle.
// close cURL resource, and free up system resources
curl_close($ch);
You can get the html of the next page with curl, and then echo the response.

add headers to file_get_contents in php

I am completely new PHP and want a client program to call an URL web service.I am using file_get_content to get the data.How do add additional headers to the request made using file_get_content.
I also was thinking of using cURL. I wanted to know how cURL can be used to do a GET request.
You can add headers to file_get_contents, it takes a parameter called context that can be used for that:
$context = stream_context_create(array(
'http' => array(
'method' => 'GET',
'header' => "Host: www.example.com\r\n" .
"Cookie: foo=bar\r\n"
)
));
$data = file_get_contents("http://www.example.com/", false, $context);
As for cURL, the basic example from the PHP manual shows you how to perform a GET request:
<?php
// 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, 0);
// grab URL and pass it to the browser
curl_exec($ch);
// close cURL resource, and free up system resources
curl_close($ch);
?>

Categories