I'm trying to use the Facebook Marketing API as detailed in this tutorial.
However I'm stumped with how to translate this suggested cURL command line request into it's PHP equivalent:
curl -G \
-d 'access_token=<ACCESS_TOKEN>' \
https://graph.facebook.com/v2.5/<LEAD_ID>
I would normally do this:
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_POST,1);
curl_setopt($ch,CURLOPT_POSTFIELDS,$access_token);
$result = curl_exec($ch);
But that produces an 'Unsupported post request' error when attempting to run it. I think I am misunderstanding what the '-G' means in the command line version?
From man curl:
-G, --get
When used, this option will make all data specified with -d, --data, --data-binary or --data-urlencode to be used in an HTTP GET request instead of the POST request that otherwise would be used. The data will be appended to the URL with a '?' separator.
There's no cURL option flag in PHP that directly corresponds to this. You can use
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
or
curl_setopt($ch, CURLOPT_HTTPGET, 'GET');
but this is hardly necessary:
CURLOPT_HTTPGET
TRUE to reset the HTTP request method to GET. Since GET is the default, this is only necessary if the request method has been changed.
You will have to specify the request parameters differently: instead of setting CURLOPT_POSTFIELDS, append them as a query string to the URL (using urlencode or the equivalent curl_escape if needed):
curl_setopt( $ch, CURLOPT_URL, $url . '?accesstoken='.urlencode('<ACCESS_TOKEN>');
Related
I can send a POST request successfully with POSTMAN and when I turn the request to code I get:
curl --location --request POST 'https://webservice.apiCommerce/import' \
--form 'id=1186' \
--form 'image=#/C:/collection/img/ecomm/01.jpg'
Now, I am trying the same thing with PHP curl and I don't get the expected result.
$imagePath = "./img/$fileName";
$post = [
"id" => $id,
"image" => '#'.$imagePath
];
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded'));
curl_setopt($ch, CURLOPT_URL, $url);
$response = curl_exec($ch);
$payload = json_decode($response, true);
The request gets sent, but the image doesn't get uploaded correctly. Is there a reason why there's a mismatch? I can't figure out what's wrong. I tried removing the header and so forth, but it doesn't seem to make any difference at all. I even hard-coded the img path and it doesn't seem to work.
The first request is succeeding because it is being encoded as multipart/form-data, as uploads should be. As the curl --help menu explains, the --form name=value command specifies multipart MIME data, as opposed to the --data option and its variants. In the second request, however, you are specifying the content type as application/x-www-urlencoding. This encoding is usually preferred, but will not in this case do what you want.
From the MDN web docs:
Non-alphanumeric characters in both keys and values are percent encoded: this is the reason why this type is not suitable to use with binary data (use multipart/form-data instead)
I am trying to translate the below cURL to php cURL:
$ curl -X POST https://tartan.plaid.com/exchange_token \
-d client_id="$plaid_client_id" \
-d secret="$plaid_secret" \
-d public_token="$public_token_from_plaid_link_module"
using this code:
$data = array(
"cliend_id"=>"test_id",
"secret"=>"test_secret",
"public_token"=>"test,fidelity,connected");
$string = http_build_query($data);
echo $string;
//initialize session
$ch=curl_init("https://tartan.plaid.com/exchange_token");
//set options
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
//execute session
$exchangeToken = curl_exec($ch);
echo $exchangeToken;
//close session
curl_close($ch);
and I am getting this response:
cliend_id=test_id&secret=test_secret&public_token=test%2Cfidelity%2Cconnected{ "code": 1100, "message": "client_id missing", "resolve": "Include your Client ID so we know who you are." }
I am not sure what is wrong with my format that is keeping plaid from recognizing the client_id portion of the post. For further reference, I have more detail below.
The below is taken from the plaid site that can be found by searching "plaid api quickstart":
Reference
/exchange_token Endpoint
The /exchange_token endpoint is available in both the tartan and production environments.
Method Endpoint Required Parameters Optional Parameters
POST /exchange_token client_id, secret, public_token account_id
The /exchange_token endpoint has already been integrated into the plaid-node, plaid-go, plaid-ruby, and plaid-python client libraries. Support for plaid-java is coming soon.
If you are working with a library that does not yet support the /exchange_token endpoint you can simply make a standard HTTP request:
$ curl -X POST https://tartan.plaid.com/exchange_token \
-d client_id="$plaid_client_id" \
-d secret="$plaid_secret" \
-d public_token="$public_token_from_plaid_link_module"
For a valid request, the API will return a JSON response similar to:
{
"access_token": "foobar_plaid_access_token"
}
The problem is you are sending cliend_id but the server expects client_id:
$data = array(
"client_id"=>"test_id", // Use client_id instead of cliend_id
"secret"=>"test_secret",
"public_token"=>"test,fidelity,connected");
I am very new to the curl and i need to send the POST request to the following url:
curl -X POST https://connect.stripe.com/oauth/token \
-d client_secret=Secret key \
-d code=AUTHORIZATION_CODE \
-d grant_type=authorization_code
in the response i will be receiving the access token from the stripe.
please let me know how can create the function in php to do the same using the php - cURL.
Thanks for your help.
Stripe would probably advise to use their libraries but this is a curl tool I've been playing with here this morning; I prefer curl over libraries that wrap around it any day:
$post = 'client_secret='.$system['stipe']['client_secret'].'&grant_type=authorization_code&code='.$_GET['code'];
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL, $system['stipe']['token_url']);
curl_setopt($ch,CURLOPT_POSTFIELDS, $post);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($ch);
curl_close($ch);
You just need to wrap it up as a function and json_decode the values returned.
This works for me whereas the code in the docs / gist didn't as they were lacking the false flag on the SSL verify peer which in my dev setup here made things prang.
I'm new with cURL and was referring this example : http://www.php.net/manual/en/book.curl.php#99979
I wanted to write the following curl request:
curl "http://localhost:8080/solr/update/extract?literal.id=doc1&commit=true" -F "myfile=#test.pdf"
using the curl predefined constants. (http://www.php.net/manual/en/curl.constants.php)
I started off by writing:
$tuCurl = curl_init();
curl_setopt($tuCurl, CURLOPT_URL, "http://localhost:8080/solr/update/extract?literal.id=doc1&commit=true");
But, after that, I couldn't figure out how to write -F "myfile=#test.pdf" part.
I'm integrating with a 3rd party's API, I have to POST some XML and I get some XML back.
On the CLI this works, I get a positive response.
curl -X POST -d #/tmp/file http://url/to/endpoint --header "Content-Type:application/x-www-form-urlencoded"
This, however, does not work, the response contains an error telling me my that my request XML is invalid.
$ch = curl_init();
$post = array(
'file' => '#/tmp/file'
);
curl_setopt($ch, CURLOPT_URL, 'http://url/to/endpoint');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type:application/x-www-form-urlencoded'));
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
$this->responseBody = curl_exec($ch);
curl_close($ch);
It's the same file in both cases and it's on the same server. the file is just plain text XML. The only difference that I can see is that I'm specifying a fieldname in my HTTP headers on the PHP version.
How do I send that file over using PHP to exactly replicate the CLI version, e.g. without the formdata/fieldname bit?
FWIW I can't go back to the developer of the API for a few days to ask what he's defining as 'bad XML'
Try passing the file as raw data, not in an array, by for example using file_get_contents().
So instead of:
$post = array('file' => '#/tmp/file');
Like this:
$post = file_get_contents('#/tmp/file');