What am I doing wrong? This is my code:
$json_url = ";
$apikey = "..........";
$data_json = '{"user":"...........",
"customer" {"username":".....","password":".....","phishing":"....."},"seller":{"username":".....","password":"......","phishing":"....."},"amount":10000,"platform":"ps"}';
$ch = curl_init($json_url);
curl_setopt_array($ch, array(
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $data_json,
CURLOPT_HEADER => true,
CURLOPT_HTTPHEADER => array(
'Connection: keep-alive',
'Accept: application/json',
'Content-Type: application/json; charset=utf-8',
'Host: futservices.com',
'Content-Length: '.strlen($data_json).'',
'Accept-Encoding: gzip'
))
);
$response = curl_exec($ch);
curl_close($ch);
echo $response;
$order = json_decode($response, true);
echo "Your Token Is: ".$order['d'];`
The request goes through fine, however, I want to grab the "d" JSON element that sent by in a JSON Response from the API.
This is the response:
HTTP/1.1 200 OK Cache-Control: private, max-age=0 Content-Length: 44 Content-Type: application/json; charset=utf-8 Server: Microsoft-IIS/8.5 X-AspNet-Version: 4.0.30319 X-Powered-By: ASP.NET Date: Sat, 25 Apr 2015 16:58:09 GMT {"d":"d922bad4-60d8-4fe4-9cba-0231328744a0"}1Your Token Is:
As you can see "d" is not echoed after "Your Token Is: ".
How can I resolve this?
You need to set the CURLOPT_RETURN_TRANSFER flag to store the result of a CURL as a variable. What's happening is that the response is just being sent straight to your browser. Add the option as I have done below.
curl_setopt_array($ch, array(
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $data_json,
CURLOPT_HEADER => true,
CURLOPT_RETURN_TRANSFER => true,
CURLOPT_HTTPHEADER => array(
'Connection: keep-alive',
'Accept: application/json',
'Content-Type: application/json; charset=utf-8',
'Host: futservices.com',
'Content-Length: '.strlen($data_json).'',
'Accept-Encoding: gzip'
))
);
I hope that helps!
Related
i have a problem whit postman i am develope a connectio to a api and i am experience a strange unsolved error.
I am try to post a excel file to a server , in Postman all woprks fine and im able to upload the file whit no problem. But if i export the code generated , they return a " Invalid File" error.
i have try all and no way to find what is going wrong here
<?php
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => 'https://eiffel-monorepo-stock.aws.paris.cl/stock/import/7707ecb8-ebc8-4f37-86f6-4a658a46becc',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS => array('file'=> new CURLFILE('/home/alakazan/Descargas/stock_1666535230.xlsx')),
CURLOPT_HTTPHEADER => array(
'Host: [[[EXCLUDED DATA]]]',
'User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:106.0) Gecko/20100101 Firefox/106.0',
'Accept: application/json, text/plain, */*',
'Accept-Language: es,es-CL;q=0.8,en-US;q=0.5,en;q=0.3',
'Accept-Encoding: gzip, deflate, br',
'Referer: [[[EXCLUDED DATA]]]',
'Authorization: Bearer [[[EXCLUDED DATA]]],
'Content-Type: multipart/form-data; boundary=---------------------------83496323188075087905603769295',
'Content-Length: 15844',
'Origin: [[[EXCLUDED DATA]]]',
'Connection: keep-alive',
'Sec-Fetch-Dest: empty',
'Sec-Fetch-Mode: no-cors',
'Sec-Fetch-Site: same-site',
'Pragma: no-cache',
'Cache-Control: no-cache'
),
));
$response = curl_exec($curl);
curl_close($curl);
echo $response;
I try to upload using console and error are Invalid file :(
{"statusCode":400,"error":true,"message":"Invalid File"}
I am trying to get the response code from the response header using cURL PHP.
When I send the request, this is the response header that is returned by MYOB AccountRight API:
HTTP/1.1 200 OK
Access-Control-Expose-Headers: Request-Context
Cache-Control: must-revalidate, private
Content-Encoding: gzip
Content-Type: application/json;charset=utf-8
Date: Thu, 20 May 2021 01:07:56 GMT
ETag: "XXXXXXXXX"
Expires: -1
Request-Context: appId=cid-v1:a4936349-ef26-4f8a-9268-XXXXXXXXX
Server: Microsoft-IIS/10.0
Vary: Accept-Encoding
X-AspNet-Version: 4.0.30319
X-Mashery-Message-ID: 2fc6b494-54e8-43e2-8bc4-XXXXXXXXX
X-Mashery-Responder: prod-j-worker-ap-southeast-2b-33.mashery.com
x-myobapi-elapsed: 1370
x-myobapi-requestid: bb0764c8-f62d-4848-bcae-XXXXXXXXX
X-Powered-By: ASP.NET
Content-Length: 1205
Connection: keep-alive
I have tried the solution from Getting HTTP code in PHP using curl , but I will not get the http code.
This is my code to get the accounts data:
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => 'https://ar1.api.myob.com/accountright/766d620e-a5eb-41c3-8343-XXXXXXXX/GeneralLedger/Account?$filter=Name%20eq%20\'Inventory\'%20or%20Name%20eq%20\'Cost%20Of%20Sales\'%20or%20Name%20eq%20\'Inventory%20Income\'',
CURLOPT_HEADER => true,
CURLOPT_NOBODY => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'GET',
CURLOPT_HTTPHEADER => array(
'x-myobapi-version: v2',
'Accept-Encoding: gzip,deflate',
'x-myobapi-key: '.$theAPIKey,
'x-myobapi-cftoken: '.$theCFToken,
'Authorization: Bearer '.$theAccessToken
)
));
$response = curl_exec($curl);
$theInfo = curl_getinfo($response);
$http_code = $theInfo['http_code'];
curl_close($curl);
echo 'http code: ' . $http_code . '<br />';
echo '<pre>';
echo $response;
echo '</pre>';
When I echo the http code, nothing will be printed.
I think you need to pass $curl to the curl_getinfo method, not the $response
$response = curl_exec($curl);
$theInfo = curl_getinfo($curl);
$http_code = $theInfo['http_code'];
You can see the doco here.. https://www.php.net/manual/en/function.curl-getinfo.php
This is the error am getting in my curl PHP
HTTP/1.1 401 Unauthorized Cache-Control: no-cache Pragma: no-cache Content-Type: application/json; charset=utf-8 Expires: -1 Server: Microsoft-IIS/10.0 X-AspNet-Version: 4.0.30319 WWW-Authenticate: Bearer WWW-Authenticate: Bearer X-Powered-By: ASP.NET Date: Thu, 23 Apr 2020 10:57:54 GMT Content-Length: 61 {"Message":"Authorization has been denied for this request."}";
The API isn't receiving an access token, I think its lacking authorization. pls help me with this below authorization code to add in my code properly, am a little bit confused where to add it.
below is the authorisation code
curl_setopt($handle1, CURLOPT_HTTPHEADER, array("Authorization: Bearer ".$access_token));
//the code I have written
<?php
$access_token = $_SESSION['token'];
$request_headers = array();
$request_headers[] = 'Bearer: ' . $access_token;
//$request_headers[]='Content-Length:150';
$handle1 = curl_init();
$api_url = 'API';
curl_setopt_array(
$handle1,
array(
CURLOPT_URL => $api_url,
CURLOPT_POST => false,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => $request_headers,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_HEADER => true,
CURLOPT_TIMEOUT => -1,
)
);
$data = curl_exec($handle1);
echo serialize($data);
?>
$request_headers[] = 'Bearer: ' . $access_token;
it seems like a typo -> $request_headers[] = 'Authorization: Bearer ' . $access_token;
I work with boomerang, but it's more or less the same as postman (i think).
With Boomerang i can do the call normally, but when i tried to do the same with php i can't.
here the two headers:
BOOMERANG / POSTMAN
GET https://example.es/api/bla/bloum/174 HTTP/1.1
Authorization: Basic ajklfsdkjfalksjdflñaskjdflakjdf=
Server: Apache-Coyote/1.1
X-Result-MaxResults: 1
X-Result-CurrentPage: 1
X-Result-MaxPages: 1
Content-Type: application/json;charset=UTF-8
Date: Fri, 28 Oct 2011 08:24:22 GMT
Transfer-Encoding: chunked
MY CODE
GET /api/bla/bloum/174 HTTP/1.1
Host: example.es
Accept: */*
Authorization: Basic ajklfsdkjfalksjdflñaskjdflakjdf=
Server: Apache-Coyote/1.1
X-Result-MaxResults: 1
X-Result-CurrentPage: 1
X-Result-MaxPages: 1
Content-Type: application/json;charset=UTF-8
Date: Fri, 28 Oct 2011 08:24:22 GMT
Transfer-Encoding: chunked
I can't put the url in the same way that Boomerang show, and i don't know if it's the problem.
POSTMAN CODE
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://example.es/api/bla/bloum/174",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_SSL_VERIFYHOST => false,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_HTTPHEADER => array(
"authorization: Basic ajklfsdkjfalksjdflñaskjdflakjdf=",
"cache-control: no-cache",
"content-type: application/json;charset=UTF-8",
"date: Fri, 28 Oct 2011 08:24:22 GMT",
"postman-token: FJKFK",
"server: Apache-Coyote/1.1",
"transfer-encoding: chunked",
"x-result-currentpage: 1",
"x-result-maxpages: 1",
"x-result-maxresults: 1"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}
UPDATE
$error = curl_error($ch); // "Empty reply from server"
$httpCode = curl_getinfo($ch , CURLINFO_HTTP_CODE); // 0
UPDATE 2.0
I use POSTMAN to genereate the code and now the error is:
"Operation timed out after 30001 milliseconds with 0 bytes received"
without:
CURLOPT_TIMEOUT => 30 //"Empty reply from server"
This code works:
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => 'https://example.es/api/asd/zxc/123',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_SSL_VERIFYHOST => false,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'GET',
CURLOPT_HTTPHEADER => array(
'authorization: Basic ghfgfiytfgfigfkgfkgjkbvjkhgkjhgkjhg',
'cache-control: no-cache',
'content-type: application/json;charset=UTF-8',
'server: Apache-Coyote/1.1',
'x-result-currentpage: 3',
'x-result-maxpages: 1',
'x-result-maxresults: 1'
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
$this->logger->error($err);
}
But i don't know why, sorry.
I export it with POSTMAN, and the second time it works...
I want to upload a video direct to Youtube from my server for which I am using PHP curl.
I need this request format:
POST /feeds/api/users/default/uploads HTTP/1.1
Host: uploads.gdata.youtube.com
Authorization: Bearer ACCESS_TOKEN
GData-Version: 2
X-GData-Key: key=adf15ee97731bca89da876c...a8dc
Slug: video-test.mp4
Content-Type: multipart/related; boundary="f93dcbA3"
Content-Length: 1941255
Connection: close
--f93dcbA3
Content-Type: application/atom+xml; charset=UTF-8
<?xml version="1.0"?>
<entry xmlns="http://www.w3.org/2005/Atom"
xmlns:media="http://search.yahoo.com/mrss/"
xmlns:yt="http://gdata.youtube.com/schemas/2007">
<media:group>
<media:title type="plain">Bad Wedding Toast</media:title>
<media:description type="plain">
I gave a bad toast at my friend's wedding.
</media:description>
<media:category
scheme="http://gdata.youtube.com/schemas/2007/categories.cat">People
</media:category>
<media:keywords>toast, wedding</media:keywords>
</media:group>
</entry>
--f93dcbA3
Content-Type: video/mp4
Content-Transfer-Encoding: binary
<Binary File Data>
--f93dcbA3--
This is what I have:
$content = $this->buildRequestContent();
$ch = curl_init();
$curlConfig = array(
CURLOPT_URL => 'http://uploads.gdata.youtube.com/feeds/api/users/default/uploads',
CURLOPT_POST => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_BINARYTRANSFER => true,
CURLOPT_POSTFIELDS => $content,
CURLOPT_HTTPHEADER, array(
"Authorization" => sprintf("GoogleLogin auth=%s", $this->accessToken),
"GData-Version" => 2,
"X-GData-Key" => sprintf("key=%s", $this->developerKey),
"Slug" => sprintf("%s", $this->video->getFilename()),
"Content-Type" => sprintf("multipart/related; boundary=\"%s\"", $this->boundaryString),
"Content-Length" => strlen($content),
"Connection" => "close"
),
);
curl_setopt_array($ch, $curlConfig);
$result = curl_exec($ch);
Dumping the result shows me that curl changed the Content-Type to application/x-www-form-urlencoded which is of course not supported by youtube.
I put my binary content (the video) into CURLOPT_POSTFIELDS, maybe this is wrong, but I don't know how to set the request body other than that.
So how do I preserve my Content-Type that I set?
Accepted answer was not helpful for me and I found another problem in your code. According to the current PHP documentation CURLOPT_HTTPHEADER must be set like this:
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-type: text/plain',
'Content-length: 100'
));
It must be a list of strings, not a hash like array('Content-Type' => 'text/plain').
Hope it will save someone's debugging time.
According to the documentation section regarding the options' parameter setting the HTTP method to POST defaults to content type being set to application/x-www-form-urlencoded. So my suspicion is that setting all the options at once, results in your Content-Type being overwritten.
My suggestion would be to set the content type in a single statement, after you've set the method, i.e.
$curlConfig = array(
CURLOPT_URL => 'http://uploads.gdata.youtube.com/feeds/api/users/default/uploads',
CURLOPT_POST => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_BINARYTRANSFER => true,
CURLOPT_POSTFIELDS => $content,
);
curl_setopt_array($ch, $curlConfig);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Authorization: ' . sprintf('GoogleLogin auth=%s', $this->accessToken),
'GData-Version: 2',
'X-GData-Key: ' . sprintf('key=%s', $this->developerKey),
'Slug: ' . sprintf('%s', $this->video->getFilename()),
'Content-Type: ' . sprintf('multipart/related; boundary="%s"',
$this->boundaryString),
'Content-Length: ' . strlen($content),
'Connection: close'
));