I use php cURL to get facebook api data. some code here. first cURL get access_token then query something below. But I get error Warning: Invalid argument supplied for foreach() then I echo a $body , back error message:
HTTP/1.1 400 Bad Request Cache-Control: no-store Content-Type: text/javascript; charset=UTF-8 Expires: Sat, 01 Jan 2000 00:00:00 GMT P3P: CP="Facebook does not have a P3P policy. Learn why here: http://fb.me/p3p" Pragma: no-cache WWW-Authenticate: OAuth "Facebook Platform" "invalid_request" "Unsupported post request." X-FB-Rev: 386815 Set-Cookie: datr=iOPoTeHtN_Q9AO5Y4LpEXwyV; expires=Sun, 02-Jun-2013 13:37:12 GMT; path=/; domain=.facebook.com; httponly X-FB-Server: 10.28.8.130 X-Cnection: close Date: Fri, 03 Jun 2011 13:37:12 GMT Content-Length: 79 {"error":{"type":"GraphMethodException","message":"Unsupported post request."}}
is the problem in cURL? Thanks.
php code
$APPID='14017XXXXXXXXXX';
$APPSECRET='7702362a5f6d605882f1beXXXXXXXXXX';
$action="https://graph.facebook.com/oauth/access_token?client_id=".$APPID."&client_secret=".$APPSECRET."&grant_type=client_credentials" ;
$ch = curl_init($action);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt($ch, CURLOPT_POST, true );
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_USERAGENT , 'Mozilla/5.0 (Windows; U; Windows NT 5.1; fr; rv:1.9.1.1) Gecko/20090715 Firefox/3.5.1');
$r=curl_exec($ch);
$access_token=substr($r,13);
//echo $access_token; // echo 14017XXXXXXXXXX|LdTccOQAcHWrT2RCwXXXXXXXXXX
curl_close($ch);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://graph.facebook.com/search?access_token=".$access_token."&q=sashimi&limit=20&type=page&scope=publish_stream,offline_access,user_status,read_stream");
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost);
$body = curl_exec($ch);
curl_close($ch);
$status_list = json_decode($body,true);
foreach ($status_list['data'] as $status_list) {
echo $status_list['id'];
}
json tree:
{
"data": [
{
"name": "Sashimi",
"category": "Food/beverages",
"id": "61615549874"
},
{
"name": "Sashimi",
"category": "Restaurant/cafe",
"id": "185314048174674"
},
... ...
],
"paging": {
"next": "https://graph.facebook.com/search?access_token=140174749371026|LdTccOQAcHWrT2RCwfVCqPVH0IY&q=SASHIMI&limit=20&type=page&scope=publish_stream\u00252Coffline_access\u00252Cuser_status\u00252Cread_stream&offset=20"
}
}
The problem is you are doing a post request but for search you need to do a get request. Curl response also showing that.
{"error":{"type":"GraphMethodException","message":"Unsupported post request."}
Just remove line curl_setopt($ch, CURLOPT_POST, 1);. It will work and it worked for me :).
Related
I'm using Shopify API with PHP CURL POST Method to create a product. Kindly find the following code and the response. I got error "Not Found".
<?php
$products_array = array(
"product"=>array(
'title'=>'',
"title"=> "Burton Custom Freestlye 151",
"body_html"=> "<strong>Good snowboard!</strong>",
"vendor"=> "Burton",
"product_type"=> "Snowboard",
"published"=> false ,
"variants"=>array(
array(
"sku"=>"t_009",
"price"=>20.00,
"grams"=>200,
"taxable"=>false,
)
)
)
);
echo json_encode($products_array);
echo "<br />";
$url = "https://apikey:password#hostname/admin/products.json";
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/json; charset=utf-8'));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_VERBOSE, 0);
curl_setopt($curl, CURLOPT_HEADER, 1);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($products_array));
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
$response = curl_exec ($curl);
curl_close ($curl);
echo "<pre>";
print_r($response);
?>
The response of the request :
{"product":{"title":"Burton Custom Freestlye 151","body_html":"Good snowboard!<\/strong>","vendor":"Burton","product_type":"Snowboard","published":false,"variants":[{"sku":"t_009","price":20,"grams":200,"taxable":false}]}}
HTTP/1.1 404 Not Found
Server: nginx
Date: Sat, 01 Apr 2017 21:08:04 GMT
Content-Type: application/json; charset=utf-8
Transfer-Encoding: chunked
Connection: keep-alive
Vary: Accept-Encoding
Vary: Accept-Encoding
Vary: Accept-Encoding
X-Frame-Options: DENY
X-Content-Type-Options: nosniff
X-Download-Options: noopen
X-Permitted-Cross-Domain-Policies: none
X-XSS-Protection: 1; mode=block; report=/xss- report?source%5Baction%5D=create&source%5Bapp%5D=Shopify& source%5Bcontroller%5D=admin%2Fproducts&source%5Bsection%5D=admin&source%5Buuid%5D=5539f94e-776f-4672-ab2a-f340c0cf2ad1
X-Dc: ash,chi2
X-Request-ID: 5539f94e-776f-4672-ab2a-f340c0cf2ad1
{"errors":"Not Found"}
How can I fix that error ? and what does that mean ??
Double check that you are using the right .myshopify.com hostname for your store. You can see this (along with the API key and password) after creating a private app in the Shopify admin: https://shopify.com/admin/apps/private
How can i get the value of fee in this response
I am integrating the postman api in the php application, for that i am using a curl call from php,
here is the code
<?php
class PostMates extends Controller {
public function getDeliveryQuote()
{
$url = "https://api.postmates.com/v1/customers/cus_XX/delivery_quotes";
$uname = "70538e7";
$pwd = "xxxx";
$process = curl_init($url);
curl_setopt($process, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded','Accept: application/json'));
curl_setopt($process, CURLOPT_HEADER, 1);
curl_setopt($process, CURLOPT_USERPWD, $uname . ":" . $pwd);
curl_setopt($process, CURLOPT_TIMEOUT, 30);
curl_setopt($process, CURLOPT_POST, 1);
curl_setopt($process, CURLOPT_POSTFIELDS, "dropoff_address=20 McAllister St, San Francisco, CA 94102&pickup_address=101 Market St, San Francisco, CA 94105");
curl_setopt($process, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($process, CURLOPT_SSL_VERIFYPEER, false);
$return = curl_exec($process);
curl_close($process);
//var_dump($return);
print_r($return);
}
}
While i do print_r and var_dump, below given is the response.
How can i get the fee amount from the resonse.
I tried like $response->fee and $resonse['fee'], but i don't get the result.
How can i get it . Help pls
HTTP/1.1 200 OK
Server: cloudflare-nginx
Date: Sun, 28 Feb 2016 18:26:14 GMT
Content-Type: application/json
Transfer-Encoding: chunked
Connection: keep-alive
Set-Cookie: __cfduid=d7e72569c2358ea8636fac1a1054815081456683973; expires=Mon, 27-Feb-17 18:26:13 GMT; path=/; domain=.postmates.com; HttpOnly
X-Content-Type-Options: nosniff
X-Frame-Options: SAMEORIGIN
X-XSS-Protection: 1; mode=block
CF-RAY: 27be2d3566b42fd5-MAA
{"kind": "delivery_quote", "fee": 750, "created": "2016-02-28T18:26:14Z", "expires": "2016-02-28T18:31:14Z", "currency": "usd", "duration": 60, "dropoff_eta": "2016-02-28T19:31:14Z", "id": "dqt_KhBvlVTh8vQ8N-"}
Here ya go. You have to parse the results.
$result = json_decode($result,true); //decodes JSON to associative array
echo $result['fee']; //750
Alternatively, if you want to use an object format
$result = json_decode($result); //decodes JSON to an object
echo $result->fee; //750
EDIT
You also need to set CURLOPT_HEADER to false
This includes response header in output
curl_setopt($process, CURLOPT_HEADER, false);
I'm trying to work off the Harvest php API example from the GitHub page.
Here is the code, but there is no output no matter what I try to do. If I use the Chrome Postman app I can retrieve data. I'm not sure what I'm doing wrong. Confidential information was removed. Any help is much appreciated.
<?php
$url = "https://company.harvestapp.com/?daily=";
$temp = getURL($url);
echo $temp;
function getURL($url) {
$credentials = "<email>:<password>";
$headers = array(
"Content-Type: application/xml",
"Accept: application/xml",
"Authorization-Basic: " . base64_encode($credentials)
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_USERAGENT, "test app site");
$data = curl_exec($ch);
curl_close($ch);
if ($data !=false){
return $data;
}
else
return "no data";
}
?>
This is the error I get from the curl_error function:
HTTP/1.1 401 Unauthorized Server: nginx Date: Fri, 09 May 2014 22:59:27 GMT Content-Type: application/xml; charset=utf-8 Transfer-Encoding: chunked Connection: keep-alive Status: 401 Unauthorized Cache-Control: private, no-store, no-cache, max-age=0, must-revalidate X-Frame-Options: SAMEORIGIN X-Served-From: https://.harvestapp.com/ X-UA-Compatible: IE=Edge,chrome=1 Set-Cookie: _harvest_sess=; domain=.harvestapp.com; path=/; secure; HttpOnly X-Request-Id: e9d6eb74-5c73-430e-9854-ebf6f1c527c8 X-Runtime: 0.019223 You must be authenticated with Harvest to complete this request.
I'm not a php developer! I'm just trying to make php curl work on my localhost, talking to my REST service...
Note: I have a self-signed certificate and I suspect php curl goes to [http://localhost:8443/myapp/oauth/token] instead of [https://localhost:8443/myapp/oauth/token]
But I couldn't figure out why?
Here is my code below;
public function oauth2() {
echo 'Starting...<br/>';
$postargs = array(
'grant_type' => 'password',
'client_id' => 'some-client',
'client_secret' => 'some-secret',
'scope' => 'play,trust',
'username' => 'tester',
'password' => '121212'
);
$url = 'https://localhost:8443/myapp/oauth/token';
try {
$this->load->library('curl');
$curl_handle = curl_init();
curl_setopt($curl_handle, CURLOPT_URL, $url);
curl_setopt($curl_handle, CURLOPT_PORT, 8443);
curl_setopt($curl_handle, CURLOPT_POST, true);
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl_handle, CURLOPT_HEADER, true);
curl_setopt($curl_handle, CURLOPT_HTTPHEADER, array('Accept: application/json'));
curl_setopt($curl_handle, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl_handle, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($curl_handle, CURLOPT_SSLVERSION, 3);
curl_setopt($curl_handle, CURLOPT_NOBODY, true);
//curl_setopt($curl_handle, CURLOPT_FOLLOWLOCATION, true);
//curl_setopt($curl_handle, CURLOPT_MAXREDIRS, 10);
//curl_setopt($curl_handle, CURLOPT_VERBOSE, true);
curl_setopt($curl_handle, CURLOPT_POSTFIELDS, $postargs);
$buffer = curl_exec($curl_handle);
if (curl_errno($curl_handle)) {
echo curl_error($curl_handle).'<br/>';
} else {
//echo curl_getinfo($curl_handle);
curl_close($curl_handle);
echo '<p>'.$buffer.'</p>';
}
} catch (Exception $e) {
echo 'Exception....<br/>';
echo $e->getMessage();
echo $e->getTraceAsString();
die;
}
echo 'Continue...<br/>';
die;
}
Above doesn't work... Why do I get "HTTP/1.1 100 Continue" in the beginning? It shouldn't be like that! Here is the response I see.
HTTP/1.1 100 Continue HTTP/1.1 401
Unauthorized Server: Apache-Coyote/1.1
Cache-Control: no-store
Pragma: no-cache
WWW-Authenticate: Basic realm="***/client",
error="unauthorized",
error_description="An Authentication object was not found in the SecurityContext"
Content-Type: application/json;
charset=UTF-8 Transfer-Encoding: chunked Date: Fri,
10 Jan 2014 04:44:40 GMT
{"error":"unauthorized","error_description":"An Authentication object was not found in the SecurityContext"}
However when I run curl from command line like below it works!
curl -k -i -H "Accept: application/json" -X POST -d "grant_type=password&client_id=some-client&client_secret=some-secret&scope=play,trust&username=tester&password=121212" [https://localhost:8443/myapp/oauth/token]
HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Cache-Control: no-store
Pragma: no-cache
Content-Type: application/json;charset=UTF-8
Transfer-Encoding: chunked
Date: Fri, 10 Jan 2014 04:56:45 GMT
{"access_token":"cee03c37-9168-4dca-8ffe-bd7f469cdcb5","token_type":"bearer","expires_in":5238,"scope":"play trust","client_id":"...."}
Found the problem!
Changing below line worked!
curl_setopt($curl_handle, CURLOPT_POSTFIELDS, "grant_type=password&client_id=....&client_secret=....&scope=play,trust&username=tester&password=121212");
Instead of an array it needed a string.
I'm trying to login to kiala website. Kiala is a "shipping" compagny and I like to get a new shipping token with php. Since there is no api for doing this I tried with curl. Now I don't have much experience with curl and I can't make curl saving the cookies to the jar. I've tried many things but now I get to the point that I want to rip my hair off. I need these cookies to make further requests.
I've made a dummy account for testing
website: http://www.kialaverzendservice.be/
login form: http://www.kialaverzendservice.be/login.required.action?os_destination=%2Fsender%2Fstart.action
email: kialatest#mailinator.com
pwd: test123
I get the following header
HTTP/1.1 200 OK Date: Thu, 10 Oct 2013 09:46:37 GMT Server:
Apache-Coyote/1.1 Content-Type: text/html;charset=ISO-8859-15 Vary:
Accept-Encoding Set-Cookie:
berkano-seraph-login=eGV5ZcKEZXhlwoZlwoJkfGJ5Y31jemTCgGJ7YsKGYsKCYnhifmLCgmLChmN6YsKGY3dmwoNiwoZjwoNjeGh+YnhjfWJ5Yn1mwoFmwoFm;
Expires=Fri, 10-Oct-2014 09:46:37 GMT; Path=/ Set-Cookie:
kiala-c2c-language=nl; Expires=Tue, 28-Oct-2081 13:00:44 GMT; Path=/
Transfer-Encoding: chunked
My simplified php code: I'm able to login but the cookies in the header are not set in my cookiejar-file? btw I'm on localhost (wamp), but I don't think it matters.
loginToKiala();
function loginToKiala(){
$url = 'http://kialaverzendservice.be/sender/start.action';
//POST vars
$fields = array(
'os_username' => urlencode('kialatest#mailinator.com'),
'os_password' => urlencode('test123'),
'os_cookie'=>urlencode('true')//remember me
);
//url-ify the data for the POST
$fields_string='';
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
rtrim($fields_string, '&');
$ch=curl_init();
$cookie_file = './cookies.txt';
if (! file_exists($cookie_file) || ! is_writable($cookie_file))
{
echo 'Cookie file missing or not writable.';
exit;
}
//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_POST, count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file);
//curl_setopt($ch,CURLOPT_AUTOREFERER, true);
//curl_setopt($ch, CURLOPT_REFERER, 'http://www.kialaverzendservice.be/');//set referer for first request
//curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.1) Gecko/20061204 Firefox/2.0.0.1");
curl_setopt($ch, CURLOPT_HEADER, 1);
//curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_exec ($ch); // execute the curl command
curl_close ($ch);
unset($ch);
}
any help is appreciated!
First of all you're not using CURLOPT_POST correctly. cURL is expecting TRUE, FALSE, 1 or 0. The function you have there, count($fields) will (probably) return a number higher than one.
Do this instead:
curl_setopt( $ch, CURLOPT_POST, 1 );
At least, if I understand correctly that you want to use POST instead of GET.