I'm trying to POST some data to a web service with cURL, using the following code:
$response = "<p>Here is your RMA information. Please ship the product to the address below.<br>
<br>
IMPORTANT:<br>
1) Refunds can only be issues for purchases made within 30 days. Products missing any accessories or original packaging will require a 20% restocking fee.<br>Please see our RMA Guidelines here:<br>
We will update you with the progress of the RMA once we receive your RMA. We appreciate your patience and understanding.<br>
</p>";
$url = "https://mysite.desk.com/api/v2/cases/18/notes";
$username = "my username";
$password = "my password";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_USERPWD, $username . ":" . $password);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, '{"body":"' . $response . '"}');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
$output = curl_exec($ch);
it gives me following error
[url] => https://mysite.desk.com/api/v2/cases/18/notes
[content_type] =>
[http_code] => 500
[header_size] => 165
[request_size] => 228
[filetime] => -1
[ssl_verify_result] => 20
[redirect_count] => 0
[total_time] => 1.373
[namelookup_time] => 0
[connect_time] => 0.281
[pretransfer_time] => 0.811
[size_upload] => 2235
[size_download] => 0
[speed_download] => 0
[speed_upload] => 1627
[download_content_length] => -1
[upload_content_length] => 2235
[starttransfer_time] => 1.092
[redirect_time] => 0
I don't understand exact problem. please help me.
try array if json format data seem to be complicated
$post_array=array('body'=>$response);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_array;
It is solved by changing following lines
$data = array();
$data['body'] = preg_replace("/&#?[a-z0-9]{2,8};/i", "", strip_tags($_POST['response']));
and
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
Related
I'm trying to get Robinhood option data in PHP, which requires authentication. I feel like I am a breath away from my solution, but after trying for a day I am ready to ask for help.
So far, I have been able to log in to Robinhood and get the token, then use that token to authenticate a request for a second (oauth) token successfully. But for some reason, I am unable to get options data for the option of my choice (MSFT Put 75 Exp 1/17/2020, found here with proper authentication https://api.robinhood.com/marketdata/options/0fd40096-9cbc-4b14-9df4-c1c9ea5f5729/ )
Here is how I login
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"https://api.robinhood.com/api-token-auth/");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "username=example#gmail.com&password=mypassword");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$server_output = curl_exec ($ch);
$result = json_decode($server_output);
$token = $result->token;
curl_close ($ch);
Then I take that token and convert it
$url = 'https://api.robinhood.com/oauth2/migrate_token/';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: Token '.$token));
$server_output = curl_exec ($ch);
$result = json_decode($server_output);
$oauth_token = $result->access_token;
curl_close ($ch);
Up to here so far so good, but I am only getting a blank response when I try the following:
$url = 'https://api.robinhood.com/marketdata/options/0fd40096-9cbc-4b14-9df4-c1c9ea5f5729/';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: Bearer '.$oauth_token));
$server_output = curl_exec ($ch);
var_dump($server_output);
curl_close ($ch);
Any help or ideas why I'm having issues on the last part would be immensely appreciated :)
EDIT: In answer to WebCode.ie, the result of print_r(curl_getinfo($ch)) is:
Array
(
[url] => https://api.robinhood.com/marketdata/options/0fd40096-
cbc-4b14-9df4-c1c9ea5f5729/
[content_type] => application/json
[http_code] => 405
[header_size] => 187
[request_size] => 453
[filetime] => -1
[ssl_verify_result] => 0
[redirect_count] => 0
[total_time] => 0.386518
[namelookup_time] => 2.8E-5
[connect_time] => 0.094845
[pretransfer_time] => 0.289176
[size_upload] => 0
[size_download] => 0
[speed_download] => 0
[speed_upload] => 0
[download_content_length] => 0
[upload_content_length] => -1
[starttransfer_time] => 0.386491
[redirect_time] => 0
[redirect_url] =>
[primary_ip] => 52.200.3.207
[certinfo] => Array
(
)
[primary_port] => 443
[local_ip] => 84.x.x.x //my IP
[local_port] => 60974
)
Looks like I was making POST call when I should have been making a GET call. If you are having trouble remove this line from my last code block.
curl_setopt($ch, CURLOPT_POST, 1);
I apologize in advance for my English.
I need from a url http://www.streamuj.tv/video/2f15014c90a9f62af511?streamuj=hd&authorize=7736cdf0f3719ed75b26132aee184525 get the final redirected to url redirection (.flv) url.
I tried this, but somehow it doesn't get the info I need:
<?php
function getMainUrl($url) {
$headers = get_headers($url, 1);
return $headers['Location'];
}
echo getMainUrl("http://www.streamuj.tv/video/2f15014c90a9f62af511?streamuj=hd&authorize=7736cdf0f3719ed75b26132aee184525");
?>
When use:
http://getlinkinfo.com/info?link=http%3A%2F%2Fwww.streamuj.tv%2Fvideo%2F2f15014c90a9f62af511%3Fstreamuj%3Dhd%26authorize%3D7736cdf0f3719ed75b26132aee184525+&x=45&y=6
it redirects to:
http://s14.streamuj.tv:8080/vid/8f18cade6df7fc2d54a3522e7515771e/58a1ecfa/2f15014c90a9f62af511_hd.flv?start=0
and this is the one I need from php
Any help would be appreciated. Thanks
Using cURL, we tell it only to fetch the header and to follow redirects.
<?php
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, 'http://www.streamuj.tv/video/2f15014c90a9f62af511?streamuj=hd&authorize=7736cdf0f3719ed75b26132aee184525');
curl_setopt($curl, CURLOPT_FILETIME, true);
curl_setopt($curl, CURLOPT_NOBODY, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HEADER, true);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, TRUE);
$header = curl_exec($curl);
$info = curl_getinfo($curl);
curl_close($curl);
print_r($info);
Outputs:
Array
(
[url] => http://s14.streamuj.tv:8080/vid/c7194f1279591f88c162382a0a5a49d1/58a1f0a5/2f15014c90a9f62af511_hd.flv?start=0
[content_type] => video/x-flv
...
)
This Work:
<?php
session_start();
include "simple_html_dom.php";
$proxy = array("88.159.43.160:80");
$proxyNum = 0;
$proxy = explode(':', $proxy[$proxyNum]);
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, 'http://www.streamuj.tv/video/2f15014c90a9f62af511?streamuj=hd&authorize=7736cdf0f3719ed75b26132aee184525');
curl_setopt($curl, CURLOPT_FILETIME, true);
curl_setopt($curl, CURLOPT_NOBODY, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HEADER, true);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($curl, CURLOPT_PROXY, $proxy[0]);
curl_setopt($curl, CURLOPT_PROXYPORT, $proxy[1]);
$header = curl_exec($curl);
$info = curl_getinfo($curl);
curl_close($curl);
print_r($info);
?>
Outputs:
Array ( [url] => http://s14.streamuj.tv:8080/vid/0b9ac442e33b2f0fe72dd45295b6e7bd/58a1f3b0/2f15014c90a9f62af511_hd.flv?start=0 [content_type] => video/x-flv [http_code] => 200 [header_size] => 738 [request_size] => 326 [filetime] => 1481886073 [ssl_verify_result] => 0 [redirect_count] => 1 [total_time] => 3.485 [namelookup_time] => 0 [connect_time] => 3.063 [pretransfer_time] => 3.063 [size_upload] => 0 [size_download] => 0 [speed_download] => 0 [speed_upload] => 0 [download_content_length] => 674855667 [upload_content_length] => -1 [starttransfer_time] => 3.188 [redirect_time] => 0.297 [redirect_url] => [primary_ip] => 88.159.43.160 [certinfo] => Array ( ) [primary_port] => 80 [local_ip] => 192.168.0.8 [local_port] => 57532 )
How to can please echo only this: http://s14.streamuj.tv:8080/vid/0b9ac442e33b2f0fe72dd45295b6e7bd/58a1f3b0/2f15014c90a9f62af511_hd.flv?start=0
?
I need to send a cURL request containing a file to upload as well as a JSON string. I can get a properly formatted request when sending the JSON, but the file is throwing it off.
$postData = array(
'JSON'=> json_encode($jsonParams),
$reference => '#'.$tmp_file_path
);
//Borrowed function from: http://scraperblog.blogspot.com/2013/07/php-curl-multipart-form-posting.html
function multipart_build($fields, $boundary){
$retval = '';
foreach($fields as $key => $value){
$retval .= "--$boundary\nContent-Disposition: form-data; name=\"$key\"\n\n$value\n";
}
$retval .= "--$boundary--";
return $retval;
}
$boundary = "--requestboundary-xxx";
$request = $this->multipart_build($postData,$boundary);
$curl = curl_init();
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); // stop verifying certificate
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, array("Content-Type: multipart/form-data; boundary=$boundary"));
curl_setopt($curl, CURLOPT_URL, $target);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $request);
$response = curl_exec($curl);
$info = curl_getinfo($curl);
curl_close($curl);
The $reference needs to be tied to a value in the $jsonParams so the receiving server can ensure uploaded files are attached to the corresponding data string packages.
////////////////////////////////////////////////////////////
Update after Marc B's comment
////////////////////////////////////////////////////////////
So my first approach wasn't using my own multipart. The reason I went with multipart was at least I was getting a response from the server. Here is the original code.
$curl_file_object = curl_file_create($attachmentFile['tmp_name'], $attachmentFile['type'], $reference);
$postData= array(
'JSON'=> json_encode($jsonParams),
$reference => $curl_file_object,
);
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $target);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); // stop verifying certificate
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HEADER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, array("Content-Type: multipart/form-data;"));
curl_setopt($curl, CURLOPT_POST, true); // enable posting
curl_setopt($curl, CURLOPT_POSTFIELDS, $postData); // post data
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true); // if any redirection after upload
$response = curl_exec($curl);
$info = curl_getinfo($curl);
curl_close($curl);
debug($info);
And with this I get:
[url] => --------
[content_type] =>
[http_code] => 0
[header_size] => 0
[request_size] => 0
[filetime] => -1
[ssl_verify_result] => 0
[redirect_count] => 0
[total_time] => 0.031
[namelookup_time] => 0
[connect_time] => 0.031
[pretransfer_time] => 0
[size_upload] => 0
[size_download] => 0
[speed_download] => 0
[speed_upload] => 0
[download_content_length] => -1
[upload_content_length] => -1
[starttransfer_time] => 0
[redirect_time] => 0
[redirect_url] =>
[primary_ip] => --------
[certinfo] => Array
(
)
[primary_port] => --------
[local_ip] => --------
[local_port] => --------
With the multipart I was at least getting a 400.
Don't built your own multipart. Curl is perfectly capable of doing that for you:
curl_setopt($curl, CURLOPT_POSTFIELDS, $postData);
it'll take an array and do whatever's needed automatically.
And if you're on PHP 5.5+, you can use curlfile for the file part as well, without the # hack.
Using CURLFile instead of curl_file_create() fixed the problem. Even though they are supposed to be the same...
I am trying to use The OneAPI SMS interface to send SMS messages from my application using cURL.
It returns 500 error and here is the code I am using:
<?php
$url = "https://oneapi-gw.gsma.com/smssend/2_0/smsmessaging/outbound/tel%3A%2B1234/requests";
$username = "secret";
$password = "secret";
$request = array(
'address' => 'tel%3A%2B1222333444',
'message' => 'hello world',
'senderAddress' => 'tel%3A%2B1234',
'senderName' => 'joe doe'
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($request));
$output = curl_exec($ch);
$info = curl_getinfo($ch);
curl_close($ch);
print_r($info);
?>
Any help woudl be any help would be greatly appreciated. Thank you!
P.S.
Array ( [url] => https://oneapi-gw.gsma.com/smssend/2_0/smsmessaging/outbound/tel%3A%2B1416XXXYYYY/requests
[content_type] => application/json [http_code] => 500 [header_size] => 494 [request_size] => 349
[filetime] => -1 [ssl_verify_result] => 0 [redirect_count] => 0 [total_time] => 0.333515 [namelookup_time] => 0.000335
[connect_time] => 0.054989 [pretransfer_time] => 0.229044 [size_upload] => 97 [size_download] => 172 [speed_download] => 515
[speed_upload] => 290 [download_content_length] => -1 [upload_content_length] => 97 [starttransfer_time] => 0.333474 [redirect_time] => 0 [certinfo] => Array ( ) )
Solved
For the ancestors: for some reasons cURL is not working, so file_get_contents() is the way to go.
If you are posting parameter values in JSON you shouldn't URL encode the values. This is necessary if using 'form post's but not for JSON.
Try making the same request using command line curl - you should be able to get the details of the server error accompanying the 500 return code.
Apparently, I solved this puzzle with cURL. The problem was in its syntax. Heres the working PHP solution for their APIs.
Class SendSMS {
public $phone_number;
public function __construct($phone_number) {
$this->phone_number = $phone_number;
}
public function oneAPI() {
$url = "https://oneapi-gw.gsma.com/smssend/2_0/smsmessaging/outbound/tel%3A7511/requests"; // END POINT
$username = 'secret'; // APP's login
$password = 'secret'; // and psswd
$request = array(
'address' => $this->phone_number,
'message' => 'hello world',
'senderAddress' => 'tel:7511'
);
$request = http_build_query($request);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
curl_setopt($ch, CURLOPT_POSTFIELDS, $request);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // hide output
$output = curl_exec($ch);
$info = curl_getinfo($ch);
curl_close($ch);
}
}
$hello = new SendSMS('+1XXXXXXXXX'); // WHITE LISTED PHONE
$hello->oneAPI();
I created an API for customers using cURL. I just moved to a new server this domain and now the api doesnt not work. Everything seems to be working fine modules wise but I can't get it to work:
This is the response I get.
Array (
[url] => https://www.1800pay.com/api/process.php
[content_type] => text/html; charset=iso-8859-1
[http_code] => 404
[header_size] => 179
[request_size] => 506
[filetime] => -1
[ssl_verify_result] => 0
[redirect_count] => 0
[total_time] => 0.038607
[namelookup_time] => 0.002688
[connect_time] => 0.002737
[pretransfer_time] => 0.038372
[size_upload] => 0
[size_download] => 294
[speed_download] => 7615
[speed_upload] => 0
[download_content_length] => 294
[upload_content_length] => 0
[starttransfer_time] => 0.038597
[redirect_time] => 0 )
Curl error:
Not Found
The requested URL /api/process.php was not found on this server.
Apache/2.2.3 (CentOS) Server at www.1800pay.com Port 443
CODE USED:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://www.1800pay.com/api/process.php");
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 15);
curl_setopt($ch, CURLOPT_TIMEOUT, 15);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
//curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST,0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_POSTFIELDS, $output_transaction);
curl_setopt($ch, CURLOPT_POST, 1);
if (!($data = curl_exec($ch))) {print_r(curl_error($ch));echo "error";
return ERROR;
}
print_r(curl_getinfo($ch));
echo 'Curl error: ' . curl_error($ch);
curl_close($ch);
print_r($data);
Thanks for the help :)
And YES THE FILE EXISTS ON THE SERVER..... :|
The answer is within the response.. a not found error is exactly that. Just make sure the file api/process.php exists on the new domain.
I'm not sure what your problem is but when I try to curl the URL using this:
function curl_get($url)
{
$curl_handle = curl_init();
curl_setopt($curl_handle, CURLOPT_URL, $url);
curl_setopt($curl_handle, CURLOPT_CONNECTTIMEOUT, 2);
curl_setopt($curl_handle, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl_handle, CURLOPT_COOKIEJAR, "cookie.txt");
curl_setopt($curl_handle, CURLOPT_COOKIEFILE, "cookie.txt");
//curl_setopt($curl_handle, CURLOPT_USERPWD, $co.":".$apikey);
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl_handle, CURLOPT_DNS_USE_GLOBAL_CACHE, FALSE);
$buffer = curl_exec($curl_handle);
$error = curl_error($curl_handle);
curl_close($curl_handle);
//if (empty($buffer))
//echo "No response from server: ".$error;
//else
return $buffer;
}
I get this:
Error [Code:1043] Payment ()Key - incorrect data